1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3
4 bttv - Bt848 frame grabber driver
5
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12
13 V4L1 removal from:
14 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
15
16 Fixes to be fully V4L2 compliant by
17 (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
18
19 Cropping and overscan support
20 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
21 Sponsored by OPQ Systems AB
22
23 */
24
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
37 #include "bttvp.h"
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-event.h>
41 #include <media/i2c/tvaudio.h>
42 #include <media/drv-intf/msp3400.h>
43
44 #include <linux/dma-mapping.h>
45
46 #include <asm/io.h>
47 #include <asm/byteorder.h>
48
49 #include <media/i2c/saa6588.h>
50
51 #define BTTV_VERSION "0.9.19"
52
53 unsigned int bttv_num; /* number of Bt848s in use */
54 struct bttv *bttvs[BTTV_MAX];
55
56 unsigned int bttv_debug;
57 unsigned int bttv_verbose = 1;
58 unsigned int bttv_gpio;
59
60 /* config variables */
61 #ifdef __BIG_ENDIAN
62 static unsigned int bigendian=1;
63 #else
64 static unsigned int bigendian;
65 #endif
66 static unsigned int radio[BTTV_MAX];
67 static unsigned int irq_debug;
68 static unsigned int gbuffers = 8;
69 static unsigned int gbufsize = 0x208000;
70 static unsigned int reset_crop = 1;
71
72 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
73 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
74 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
75 static int debug_latency;
76 static int disable_ir;
77
78 static unsigned int fdsr;
79
80 /* options */
81 static unsigned int combfilter;
82 static unsigned int lumafilter;
83 static unsigned int automute = 1;
84 static unsigned int chroma_agc;
85 static unsigned int agc_crush = 1;
86 static unsigned int whitecrush_upper = 0xCF;
87 static unsigned int whitecrush_lower = 0x7F;
88 static unsigned int vcr_hack;
89 static unsigned int irq_iswitch;
90 static unsigned int uv_ratio = 50;
91 static unsigned int full_luma_range;
92 static unsigned int coring;
93
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2 = 1;
96
97 /* insmod args */
98 module_param(bttv_verbose, int, 0644);
99 module_param(bttv_gpio, int, 0644);
100 module_param(bttv_debug, int, 0644);
101 module_param(irq_debug, int, 0644);
102 module_param(debug_latency, int, 0644);
103 module_param(disable_ir, int, 0444);
104
105 module_param(fdsr, int, 0444);
106 module_param(gbuffers, int, 0444);
107 module_param(gbufsize, int, 0444);
108 module_param(reset_crop, int, 0444);
109
110 module_param(v4l2, int, 0644);
111 module_param(bigendian, int, 0644);
112 module_param(irq_iswitch, int, 0644);
113 module_param(combfilter, int, 0444);
114 module_param(lumafilter, int, 0444);
115 module_param(automute, int, 0444);
116 module_param(chroma_agc, int, 0444);
117 module_param(agc_crush, int, 0444);
118 module_param(whitecrush_upper, int, 0444);
119 module_param(whitecrush_lower, int, 0444);
120 module_param(vcr_hack, int, 0444);
121 module_param(uv_ratio, int, 0444);
122 module_param(full_luma_range, int, 0444);
123 module_param(coring, int, 0444);
124
125 module_param_array(radio, int, NULL, 0444);
126 module_param_array(video_nr, int, NULL, 0444);
127 module_param_array(radio_nr, int, NULL, 0444);
128 module_param_array(vbi_nr, int, NULL, 0444);
129
130 MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
131 MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
132 MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
133 MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
134 MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
136 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
137 MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
138 MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
139 MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
140 MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
141 MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
142 MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
143 MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
144 MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
145 MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146 MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
147 MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
148 MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
149 MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
150 MODULE_PARM_DESC(video_nr, "video device numbers");
151 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
152 MODULE_PARM_DESC(radio_nr, "radio device numbers");
153
154 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
155 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
156 MODULE_LICENSE("GPL");
157 MODULE_VERSION(BTTV_VERSION);
158
159 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
160 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
161 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
162 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
163 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
164 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
165 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
166 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
167 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
168 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
169
170 /* ----------------------------------------------------------------------- */
171 /* sysfs */
172
card_show(struct device * cd,struct device_attribute * attr,char * buf)173 static ssize_t card_show(struct device *cd,
174 struct device_attribute *attr, char *buf)
175 {
176 struct video_device *vfd = to_video_device(cd);
177 struct bttv *btv = video_get_drvdata(vfd);
178 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
179 }
180 static DEVICE_ATTR_RO(card);
181
182 /* ----------------------------------------------------------------------- */
183 /* dvb auto-load setup */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
request_module_async(struct work_struct * work)185 static void request_module_async(struct work_struct *work)
186 {
187 request_module("dvb-bt8xx");
188 }
189
request_modules(struct bttv * dev)190 static void request_modules(struct bttv *dev)
191 {
192 INIT_WORK(&dev->request_module_wk, request_module_async);
193 schedule_work(&dev->request_module_wk);
194 }
195
flush_request_modules(struct bttv * dev)196 static void flush_request_modules(struct bttv *dev)
197 {
198 flush_work(&dev->request_module_wk);
199 }
200 #else
201 #define request_modules(dev)
202 #define flush_request_modules(dev) do {} while(0)
203 #endif /* CONFIG_MODULES */
204
205
206 /* ----------------------------------------------------------------------- */
207 /* static data */
208
209 /* special timing tables from conexant... */
210 static u8 SRAM_Table[][60] =
211 {
212 /* PAL digital input over GPIO[7:0] */
213 {
214 45, // 45 bytes following
215 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
216 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
217 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
218 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
219 0x37,0x00,0xAF,0x21,0x00
220 },
221 /* NTSC digital input over GPIO[7:0] */
222 {
223 51, // 51 bytes following
224 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
225 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
226 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
227 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
228 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
229 0x00,
230 },
231 // TGB_NTSC392 // quartzsight
232 // This table has been modified to be used for Fusion Rev D
233 {
234 0x2A, // size of table = 42
235 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
236 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
237 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
238 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
239 0x20, 0x00
240 }
241 };
242
243 /* minhdelayx1 first video pixel we can capture on a line and
244 hdelayx1 start of active video, both relative to rising edge of
245 /HRESET pulse (0H) in 1 / fCLKx1.
246 swidth width of active video and
247 totalwidth total line width, both in 1 / fCLKx1.
248 sqwidth total line width in square pixels.
249 vdelay start of active video in 2 * field lines relative to
250 trailing edge of /VRESET pulse (VDELAY register).
251 sheight height of active video in 2 * field lines.
252 extraheight Added to sheight for cropcap.bounds.height only
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, extraheight, 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) + (extraheight) + (vdelay) - \
264 MIN_VDELAY, \
265 .cropcap.defrect.left = hdelayx1, \
266 .cropcap.defrect.top = (videostart0) * 2, \
267 .cropcap.defrect.width = swidth, \
268 .cropcap.defrect.height = sheight, \
269 .cropcap.pixelaspect.numerator = totalwidth, \
270 .cropcap.pixelaspect.denominator = sqwidth,
271
272 const struct bttv_tvnorm bttv_tvnorms[] = {
273 /* PAL-BDGHI */
274 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
275 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
276 {
277 .v4l2_id = V4L2_STD_PAL,
278 .name = "PAL",
279 .Fsc = 35468950,
280 .swidth = 924,
281 .sheight = 576,
282 .totalwidth = 1135,
283 .adelay = 0x7f,
284 .bdelay = 0x72,
285 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
286 .scaledtwidth = 1135,
287 .hdelayx1 = 186,
288 .hactivex1 = 924,
289 .vdelay = 0x20,
290 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
291 .sram = 0,
292 /* ITU-R frame line number of the first VBI line
293 we can capture, of the first and second field.
294 The last line is determined by cropcap.bounds. */
295 .vbistart = { 7, 320 },
296 CROPCAP(/* minhdelayx1 */ 68,
297 /* hdelayx1 */ 186,
298 /* Should be (768 * 1135 + 944 / 2) / 944.
299 cropcap.defrect is used for image width
300 checks, so we keep the old value 924. */
301 /* swidth */ 924,
302 /* totalwidth */ 1135,
303 /* sqwidth */ 944,
304 /* vdelay */ 0x20,
305 /* sheight */ 576,
306 /* bt878 (and bt848?) can capture another
307 line below active video. */
308 /* extraheight */ 2,
309 /* videostart0 */ 23)
310 },{
311 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
312 .name = "NTSC",
313 .Fsc = 28636363,
314 .swidth = 768,
315 .sheight = 480,
316 .totalwidth = 910,
317 .adelay = 0x68,
318 .bdelay = 0x5d,
319 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
320 .scaledtwidth = 910,
321 .hdelayx1 = 128,
322 .hactivex1 = 910,
323 .vdelay = 0x1a,
324 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
325 .sram = 1,
326 .vbistart = { 10, 273 },
327 CROPCAP(/* minhdelayx1 */ 68,
328 /* hdelayx1 */ 128,
329 /* Should be (640 * 910 + 780 / 2) / 780? */
330 /* swidth */ 768,
331 /* totalwidth */ 910,
332 /* sqwidth */ 780,
333 /* vdelay */ 0x1a,
334 /* sheight */ 480,
335 /* extraheight */ 0,
336 /* videostart0 */ 23)
337 },{
338 .v4l2_id = V4L2_STD_SECAM,
339 .name = "SECAM",
340 .Fsc = 35468950,
341 .swidth = 924,
342 .sheight = 576,
343 .totalwidth = 1135,
344 .adelay = 0x7f,
345 .bdelay = 0xb0,
346 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
347 .scaledtwidth = 1135,
348 .hdelayx1 = 186,
349 .hactivex1 = 922,
350 .vdelay = 0x20,
351 .vbipack = 255,
352 .sram = 0, /* like PAL, correct? */
353 .vbistart = { 7, 320 },
354 CROPCAP(/* minhdelayx1 */ 68,
355 /* hdelayx1 */ 186,
356 /* swidth */ 924,
357 /* totalwidth */ 1135,
358 /* sqwidth */ 944,
359 /* vdelay */ 0x20,
360 /* sheight */ 576,
361 /* extraheight */ 0,
362 /* videostart0 */ 23)
363 },{
364 .v4l2_id = V4L2_STD_PAL_Nc,
365 .name = "PAL-Nc",
366 .Fsc = 28636363,
367 .swidth = 640,
368 .sheight = 576,
369 .totalwidth = 910,
370 .adelay = 0x68,
371 .bdelay = 0x5d,
372 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
373 .scaledtwidth = 780,
374 .hdelayx1 = 130,
375 .hactivex1 = 734,
376 .vdelay = 0x1a,
377 .vbipack = 144,
378 .sram = -1,
379 .vbistart = { 7, 320 },
380 CROPCAP(/* minhdelayx1 */ 68,
381 /* hdelayx1 */ 130,
382 /* swidth */ (640 * 910 + 780 / 2) / 780,
383 /* totalwidth */ 910,
384 /* sqwidth */ 780,
385 /* vdelay */ 0x1a,
386 /* sheight */ 576,
387 /* extraheight */ 0,
388 /* videostart0 */ 23)
389 },{
390 .v4l2_id = V4L2_STD_PAL_M,
391 .name = "PAL-M",
392 .Fsc = 28636363,
393 .swidth = 640,
394 .sheight = 480,
395 .totalwidth = 910,
396 .adelay = 0x68,
397 .bdelay = 0x5d,
398 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
399 .scaledtwidth = 780,
400 .hdelayx1 = 135,
401 .hactivex1 = 754,
402 .vdelay = 0x1a,
403 .vbipack = 144,
404 .sram = -1,
405 .vbistart = { 10, 273 },
406 CROPCAP(/* minhdelayx1 */ 68,
407 /* hdelayx1 */ 135,
408 /* swidth */ (640 * 910 + 780 / 2) / 780,
409 /* totalwidth */ 910,
410 /* sqwidth */ 780,
411 /* vdelay */ 0x1a,
412 /* sheight */ 480,
413 /* extraheight */ 0,
414 /* videostart0 */ 23)
415 },{
416 .v4l2_id = V4L2_STD_PAL_N,
417 .name = "PAL-N",
418 .Fsc = 35468950,
419 .swidth = 768,
420 .sheight = 576,
421 .totalwidth = 1135,
422 .adelay = 0x7f,
423 .bdelay = 0x72,
424 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
425 .scaledtwidth = 944,
426 .hdelayx1 = 186,
427 .hactivex1 = 922,
428 .vdelay = 0x20,
429 .vbipack = 144,
430 .sram = -1,
431 .vbistart = { 7, 320 },
432 CROPCAP(/* minhdelayx1 */ 68,
433 /* hdelayx1 */ 186,
434 /* swidth */ (768 * 1135 + 944 / 2) / 944,
435 /* totalwidth */ 1135,
436 /* sqwidth */ 944,
437 /* vdelay */ 0x20,
438 /* sheight */ 576,
439 /* extraheight */ 0,
440 /* videostart0 */ 23)
441 },{
442 .v4l2_id = V4L2_STD_NTSC_M_JP,
443 .name = "NTSC-JP",
444 .Fsc = 28636363,
445 .swidth = 640,
446 .sheight = 480,
447 .totalwidth = 910,
448 .adelay = 0x68,
449 .bdelay = 0x5d,
450 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
451 .scaledtwidth = 780,
452 .hdelayx1 = 135,
453 .hactivex1 = 754,
454 .vdelay = 0x16,
455 .vbipack = 144,
456 .sram = -1,
457 .vbistart = { 10, 273 },
458 CROPCAP(/* minhdelayx1 */ 68,
459 /* hdelayx1 */ 135,
460 /* swidth */ (640 * 910 + 780 / 2) / 780,
461 /* totalwidth */ 910,
462 /* sqwidth */ 780,
463 /* vdelay */ 0x16,
464 /* sheight */ 480,
465 /* extraheight */ 0,
466 /* videostart0 */ 23)
467 },{
468 /* that one hopefully works with the strange timing
469 * which video recorders produce when playing a NTSC
470 * tape on a PAL TV ... */
471 .v4l2_id = V4L2_STD_PAL_60,
472 .name = "PAL-60",
473 .Fsc = 35468950,
474 .swidth = 924,
475 .sheight = 480,
476 .totalwidth = 1135,
477 .adelay = 0x7f,
478 .bdelay = 0x72,
479 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
480 .scaledtwidth = 1135,
481 .hdelayx1 = 186,
482 .hactivex1 = 924,
483 .vdelay = 0x1a,
484 .vbipack = 255,
485 .vtotal = 524,
486 .sram = -1,
487 .vbistart = { 10, 273 },
488 CROPCAP(/* minhdelayx1 */ 68,
489 /* hdelayx1 */ 186,
490 /* swidth */ 924,
491 /* totalwidth */ 1135,
492 /* sqwidth */ 944,
493 /* vdelay */ 0x1a,
494 /* sheight */ 480,
495 /* extraheight */ 0,
496 /* videostart0 */ 23)
497 }
498 };
499 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
500
501 /* ----------------------------------------------------------------------- */
502 /* bttv format list
503 packed pixel formats must come first */
504 static const struct bttv_format formats[] = {
505 {
506 .fourcc = V4L2_PIX_FMT_GREY,
507 .btformat = BT848_COLOR_FMT_Y8,
508 .depth = 8,
509 .flags = FORMAT_FLAGS_PACKED,
510 },{
511 .fourcc = V4L2_PIX_FMT_HI240,
512 .btformat = BT848_COLOR_FMT_RGB8,
513 .depth = 8,
514 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
515 },{
516 .fourcc = V4L2_PIX_FMT_RGB555,
517 .btformat = BT848_COLOR_FMT_RGB15,
518 .depth = 16,
519 .flags = FORMAT_FLAGS_PACKED,
520 },{
521 .fourcc = V4L2_PIX_FMT_RGB555X,
522 .btformat = BT848_COLOR_FMT_RGB15,
523 .btswap = 0x03, /* byteswap */
524 .depth = 16,
525 .flags = FORMAT_FLAGS_PACKED,
526 },{
527 .fourcc = V4L2_PIX_FMT_RGB565,
528 .btformat = BT848_COLOR_FMT_RGB16,
529 .depth = 16,
530 .flags = FORMAT_FLAGS_PACKED,
531 },{
532 .fourcc = V4L2_PIX_FMT_RGB565X,
533 .btformat = BT848_COLOR_FMT_RGB16,
534 .btswap = 0x03, /* byteswap */
535 .depth = 16,
536 .flags = FORMAT_FLAGS_PACKED,
537 },{
538 .fourcc = V4L2_PIX_FMT_BGR24,
539 .btformat = BT848_COLOR_FMT_RGB24,
540 .depth = 24,
541 .flags = FORMAT_FLAGS_PACKED,
542 },{
543 .fourcc = V4L2_PIX_FMT_BGR32,
544 .btformat = BT848_COLOR_FMT_RGB32,
545 .depth = 32,
546 .flags = FORMAT_FLAGS_PACKED,
547 },{
548 .fourcc = V4L2_PIX_FMT_RGB32,
549 .btformat = BT848_COLOR_FMT_RGB32,
550 .btswap = 0x0f, /* byte+word swap */
551 .depth = 32,
552 .flags = FORMAT_FLAGS_PACKED,
553 },{
554 .fourcc = V4L2_PIX_FMT_YUYV,
555 .btformat = BT848_COLOR_FMT_YUY2,
556 .depth = 16,
557 .flags = FORMAT_FLAGS_PACKED,
558 },{
559 .fourcc = V4L2_PIX_FMT_UYVY,
560 .btformat = BT848_COLOR_FMT_YUY2,
561 .btswap = 0x03, /* byteswap */
562 .depth = 16,
563 .flags = FORMAT_FLAGS_PACKED,
564 },{
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PLANAR,
569 .hshift = 1,
570 .vshift = 0,
571 },{
572 .fourcc = V4L2_PIX_FMT_YUV420,
573 .btformat = BT848_COLOR_FMT_YCrCb422,
574 .depth = 12,
575 .flags = FORMAT_FLAGS_PLANAR,
576 .hshift = 1,
577 .vshift = 1,
578 },{
579 .fourcc = V4L2_PIX_FMT_YVU420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
581 .depth = 12,
582 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
583 .hshift = 1,
584 .vshift = 1,
585 },{
586 .fourcc = V4L2_PIX_FMT_YUV411P,
587 .btformat = BT848_COLOR_FMT_YCrCb411,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR,
590 .hshift = 2,
591 .vshift = 0,
592 },{
593 .fourcc = V4L2_PIX_FMT_YUV410,
594 .btformat = BT848_COLOR_FMT_YCrCb411,
595 .depth = 9,
596 .flags = FORMAT_FLAGS_PLANAR,
597 .hshift = 2,
598 .vshift = 2,
599 },{
600 .fourcc = V4L2_PIX_FMT_YVU410,
601 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .depth = 9,
603 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
604 .hshift = 2,
605 .vshift = 2,
606 },{
607 .fourcc = -1,
608 .btformat = BT848_COLOR_FMT_RAW,
609 .depth = 8,
610 .flags = FORMAT_FLAGS_RAW,
611 }
612 };
613 static const unsigned int FORMATS = ARRAY_SIZE(formats);
614
615 /* ----------------------------------------------------------------------- */
616 /* resource management */
617
618 /*
619 RESOURCE_ allocated by freed by
620
621 VIDEO_READ bttv_read 1) bttv_read 2)
622
623 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
624 VIDIOC_QBUF 1) bttv_release
625 VIDIOCMCAPTURE 1)
626
627 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
628 VIDIOC_QBUF 1) bttv_release
629 bttv_read, bttv_poll 1) 3)
630
631 1) The resource must be allocated when we enter buffer prepare functions
632 and remain allocated while buffers are in the DMA queue.
633 2) This is a single frame read.
634 3) This is a continuous read, implies VIDIOC_STREAMON.
635
636 Note this driver permits video input and standard changes regardless if
637 resources are allocated.
638 */
639
640 #define VBI_RESOURCES (RESOURCE_VBI)
641 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
642 RESOURCE_VIDEO_STREAM)
643
check_alloc_btres_lock(struct bttv * btv,int bit)644 int check_alloc_btres_lock(struct bttv *btv, int bit)
645 {
646 int xbits; /* mutual exclusive resources */
647
648 xbits = bit;
649 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
650 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
651
652 /* is it free? */
653 if (btv->resources & xbits) {
654 /* no, someone else uses it */
655 goto fail;
656 }
657
658 if ((bit & VIDEO_RESOURCES)
659 && 0 == (btv->resources & VIDEO_RESOURCES)) {
660 /* Do crop - use current, don't - use default parameters. */
661 __s32 top = btv->crop[!!btv->do_crop].rect.top;
662
663 if (btv->vbi_end > top)
664 goto fail;
665
666 /* We cannot capture the same line as video and VBI data.
667 Claim scan lines crop[].rect.top to bottom. */
668 btv->crop_start = top;
669 } else if (bit & VBI_RESOURCES) {
670 __s32 end = btv->vbi_fmt.end;
671
672 if (end > btv->crop_start)
673 goto fail;
674
675 /* Claim scan lines above btv->vbi_fmt.end. */
676 btv->vbi_end = end;
677 }
678
679 /* it's free, grab it */
680 btv->resources |= bit;
681 return 1;
682
683 fail:
684 return 0;
685 }
686
687 static
check_btres(struct bttv * btv,int bit)688 int check_btres(struct bttv *btv, int bit)
689 {
690 return (btv->resources & bit);
691 }
692
693 static
locked_btres(struct bttv * btv,int bit)694 int locked_btres(struct bttv *btv, int bit)
695 {
696 return (btv->resources & bit);
697 }
698
699 /* Call with btv->lock down. */
700 static void
disclaim_vbi_lines(struct bttv * btv)701 disclaim_vbi_lines(struct bttv *btv)
702 {
703 btv->vbi_end = 0;
704 }
705
706 /* Call with btv->lock down. */
707 static void
disclaim_video_lines(struct bttv * btv)708 disclaim_video_lines(struct bttv *btv)
709 {
710 const struct bttv_tvnorm *tvnorm;
711 u8 crop;
712
713 tvnorm = &bttv_tvnorms[btv->tvnorm];
714 btv->crop_start = tvnorm->cropcap.bounds.top
715 + tvnorm->cropcap.bounds.height;
716
717 /* VBI capturing ends at VDELAY, start of video capturing, no
718 matter how many lines the VBI RISC program expects. When video
719 capturing is off, it shall no longer "preempt" VBI capturing,
720 so we set VDELAY to maximum. */
721 crop = btread(BT848_E_CROP) | 0xc0;
722 btwrite(crop, BT848_E_CROP);
723 btwrite(0xfe, BT848_E_VDELAY_LO);
724 btwrite(crop, BT848_O_CROP);
725 btwrite(0xfe, BT848_O_VDELAY_LO);
726 }
727
free_btres_lock(struct bttv * btv,int bits)728 void free_btres_lock(struct bttv *btv, int bits)
729 {
730 if ((btv->resources & bits) != bits) {
731 /* trying to free resources not allocated by us ... */
732 pr_err("BUG! (btres)\n");
733 }
734 btv->resources &= ~bits;
735
736 bits = btv->resources;
737
738 if (0 == (bits & VIDEO_RESOURCES))
739 disclaim_video_lines(btv);
740
741 if (0 == (bits & VBI_RESOURCES))
742 disclaim_vbi_lines(btv);
743 }
744
745 /* ----------------------------------------------------------------------- */
746 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
747
748 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
749 PLL_X = Reference pre-divider (0=1, 1=2)
750 PLL_C = Post divider (0=6, 1=4)
751 PLL_I = Integer input
752 PLL_F = Fractional input
753
754 F_input = 28.636363 MHz:
755 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
756 */
757
set_pll_freq(struct bttv * btv,unsigned int fin,unsigned int fout)758 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
759 {
760 unsigned char fl, fh, fi;
761
762 /* prevent overflows */
763 fin/=4;
764 fout/=4;
765
766 fout*=12;
767 fi=fout/fin;
768
769 fout=(fout%fin)*256;
770 fh=fout/fin;
771
772 fout=(fout%fin)*256;
773 fl=fout/fin;
774
775 btwrite(fl, BT848_PLL_F_LO);
776 btwrite(fh, BT848_PLL_F_HI);
777 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
778 }
779
set_pll(struct bttv * btv)780 static void set_pll(struct bttv *btv)
781 {
782 int i;
783
784 if (!btv->pll.pll_crystal)
785 return;
786
787 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
788 dprintk("%d: PLL: no change required\n", btv->c.nr);
789 return;
790 }
791
792 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
793 /* no PLL needed */
794 if (btv->pll.pll_current == 0)
795 return;
796 if (bttv_verbose)
797 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
798 btv->c.nr, btv->pll.pll_ifreq);
799 btwrite(0x00,BT848_TGCTRL);
800 btwrite(0x00,BT848_PLL_XCI);
801 btv->pll.pll_current = 0;
802 return;
803 }
804
805 if (bttv_verbose)
806 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
807 btv->c.nr,
808 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
809 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
810
811 for (i=0; i<10; i++) {
812 /* Let other people run while the PLL stabilizes */
813 msleep(10);
814
815 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
816 btwrite(0,BT848_DSTATUS);
817 } else {
818 btwrite(0x08,BT848_TGCTRL);
819 btv->pll.pll_current = btv->pll.pll_ofreq;
820 if (bttv_verbose)
821 pr_info("PLL set ok\n");
822 return;
823 }
824 }
825 btv->pll.pll_current = -1;
826 if (bttv_verbose)
827 pr_info("Setting PLL failed\n");
828 return;
829 }
830
831 /* used to switch between the bt848's analog/digital video capture modes */
bt848A_set_timing(struct bttv * btv)832 static void bt848A_set_timing(struct bttv *btv)
833 {
834 int i, len;
835 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
836 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
837
838 if (btv->input == btv->dig) {
839 dprintk("%d: load digital timing table (table_idx=%d)\n",
840 btv->c.nr,table_idx);
841
842 /* timing change...reset timing generator address */
843 btwrite(0x00, BT848_TGCTRL);
844 btwrite(0x02, BT848_TGCTRL);
845 btwrite(0x00, BT848_TGCTRL);
846
847 len=SRAM_Table[table_idx][0];
848 for(i = 1; i <= len; i++)
849 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
850 btv->pll.pll_ofreq = 27000000;
851
852 set_pll(btv);
853 btwrite(0x11, BT848_TGCTRL);
854 btwrite(0x41, BT848_DVSIF);
855 } else {
856 btv->pll.pll_ofreq = fsc;
857 set_pll(btv);
858 btwrite(0x0, BT848_DVSIF);
859 }
860 }
861
862 /* ----------------------------------------------------------------------- */
863
bt848_bright(struct bttv * btv,int bright)864 static void bt848_bright(struct bttv *btv, int bright)
865 {
866 int value;
867
868 // printk("set bright: %d\n", bright); // DEBUG
869 btv->bright = bright;
870
871 /* We want -128 to 127 we get 0-65535 */
872 value = (bright >> 8) - 128;
873 btwrite(value & 0xff, BT848_BRIGHT);
874 }
875
bt848_hue(struct bttv * btv,int hue)876 static void bt848_hue(struct bttv *btv, int hue)
877 {
878 int value;
879
880 btv->hue = hue;
881
882 /* -128 to 127 */
883 value = (hue >> 8) - 128;
884 btwrite(value & 0xff, BT848_HUE);
885 }
886
bt848_contrast(struct bttv * btv,int cont)887 static void bt848_contrast(struct bttv *btv, int cont)
888 {
889 int value,hibit;
890
891 btv->contrast = cont;
892
893 /* 0-511 */
894 value = (cont >> 7);
895 hibit = (value >> 6) & 4;
896 btwrite(value & 0xff, BT848_CONTRAST_LO);
897 btaor(hibit, ~4, BT848_E_CONTROL);
898 btaor(hibit, ~4, BT848_O_CONTROL);
899 }
900
bt848_sat(struct bttv * btv,int color)901 static void bt848_sat(struct bttv *btv, int color)
902 {
903 int val_u,val_v,hibits;
904
905 btv->saturation = color;
906
907 /* 0-511 for the color */
908 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
909 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
910 hibits = (val_u >> 7) & 2;
911 hibits |= (val_v >> 8) & 1;
912 btwrite(val_u & 0xff, BT848_SAT_U_LO);
913 btwrite(val_v & 0xff, BT848_SAT_V_LO);
914 btaor(hibits, ~3, BT848_E_CONTROL);
915 btaor(hibits, ~3, BT848_O_CONTROL);
916 }
917
918 /* ----------------------------------------------------------------------- */
919
920 static int
video_mux(struct bttv * btv,unsigned int input)921 video_mux(struct bttv *btv, unsigned int input)
922 {
923 int mux,mask2;
924
925 if (input >= bttv_tvcards[btv->c.type].video_inputs)
926 return -EINVAL;
927
928 /* needed by RemoteVideo MX */
929 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
930 if (mask2)
931 gpio_inout(mask2,mask2);
932
933 if (input == btv->svhs) {
934 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
935 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
936 } else {
937 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
938 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
939 }
940 mux = bttv_muxsel(btv, input);
941 btaor(mux<<5, ~(3<<5), BT848_IFORM);
942 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
943
944 /* card specific hook */
945 if(bttv_tvcards[btv->c.type].muxsel_hook)
946 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
947 return 0;
948 }
949
950 static char *audio_modes[] = {
951 "audio: tuner", "audio: radio", "audio: extern",
952 "audio: intern", "audio: mute"
953 };
954
955 static void
audio_mux_gpio(struct bttv * btv,int input,int mute)956 audio_mux_gpio(struct bttv *btv, int input, int mute)
957 {
958 int gpio_val, signal, mute_gpio;
959
960 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
961 bttv_tvcards[btv->c.type].gpiomask);
962 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
963
964 /* automute */
965 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
966 && !btv->has_radio_tuner);
967
968 if (mute_gpio)
969 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
970 else
971 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
972
973 switch (btv->c.type) {
974 case BTTV_BOARD_VOODOOTV_FM:
975 case BTTV_BOARD_VOODOOTV_200:
976 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
977 break;
978
979 default:
980 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
981 }
982
983 if (bttv_gpio)
984 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
985 }
986
987 static int
audio_mute(struct bttv * btv,int mute)988 audio_mute(struct bttv *btv, int mute)
989 {
990 struct v4l2_ctrl *ctrl;
991
992 audio_mux_gpio(btv, btv->audio_input, mute);
993
994 if (btv->sd_msp34xx) {
995 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
996 if (ctrl)
997 v4l2_ctrl_s_ctrl(ctrl, mute);
998 }
999 if (btv->sd_tvaudio) {
1000 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1001 if (ctrl)
1002 v4l2_ctrl_s_ctrl(ctrl, mute);
1003 }
1004 if (btv->sd_tda7432) {
1005 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1006 if (ctrl)
1007 v4l2_ctrl_s_ctrl(ctrl, mute);
1008 }
1009 return 0;
1010 }
1011
1012 static int
audio_input(struct bttv * btv,int input)1013 audio_input(struct bttv *btv, int input)
1014 {
1015 audio_mux_gpio(btv, input, btv->mute);
1016
1017 if (btv->sd_msp34xx) {
1018 u32 in;
1019
1020 /* Note: the inputs tuner/radio/extern/intern are translated
1021 to msp routings. This assumes common behavior for all msp3400
1022 based TV cards. When this assumption fails, then the
1023 specific MSP routing must be added to the card table.
1024 For now this is sufficient. */
1025 switch (input) {
1026 case TVAUDIO_INPUT_RADIO:
1027 /* Some boards need the msp do to the radio demod */
1028 if (btv->radio_uses_msp_demodulator) {
1029 in = MSP_INPUT_DEFAULT;
1030 break;
1031 }
1032 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1033 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1034 break;
1035 case TVAUDIO_INPUT_EXTERN:
1036 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1037 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1038 break;
1039 case TVAUDIO_INPUT_INTERN:
1040 /* Yes, this is the same input as for RADIO. I doubt
1041 if this is ever used. The only board with an INTERN
1042 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1043 that was tested. My guess is that the whole INTERN
1044 input does not work. */
1045 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1046 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1047 break;
1048 case TVAUDIO_INPUT_TUNER:
1049 default:
1050 /* This is the only card that uses TUNER2, and afaik,
1051 is the only difference between the VOODOOTV_FM
1052 and VOODOOTV_200 */
1053 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1054 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1055 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1056 else
1057 in = MSP_INPUT_DEFAULT;
1058 break;
1059 }
1060 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1061 in, MSP_OUTPUT_DEFAULT, 0);
1062 }
1063 if (btv->sd_tvaudio) {
1064 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1065 input, 0, 0);
1066 }
1067 return 0;
1068 }
1069
1070 static void
bttv_crop_calc_limits(struct bttv_crop * c)1071 bttv_crop_calc_limits(struct bttv_crop *c)
1072 {
1073 /* Scale factor min. 1:1, max. 16:1. Min. image size
1074 48 x 32. Scaled width must be a multiple of 4. */
1075
1076 if (1) {
1077 /* For bug compatibility with VIDIOCGCAP and image
1078 size checks in earlier driver versions. */
1079 c->min_scaled_width = 48;
1080 c->min_scaled_height = 32;
1081 } else {
1082 c->min_scaled_width =
1083 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1084 c->min_scaled_height =
1085 max_t(unsigned int, 32, c->rect.height >> 4);
1086 }
1087
1088 c->max_scaled_width = c->rect.width & ~3;
1089 c->max_scaled_height = c->rect.height;
1090 }
1091
1092 static void
bttv_crop_reset(struct bttv_crop * c,unsigned int norm)1093 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1094 {
1095 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1096 bttv_crop_calc_limits(c);
1097 }
1098
1099 /* Call with btv->lock down. */
1100 static int
set_tvnorm(struct bttv * btv,unsigned int norm)1101 set_tvnorm(struct bttv *btv, unsigned int norm)
1102 {
1103 const struct bttv_tvnorm *tvnorm;
1104 v4l2_std_id id;
1105
1106 WARN_ON(norm >= BTTV_TVNORMS);
1107 WARN_ON(btv->tvnorm >= BTTV_TVNORMS);
1108
1109 tvnorm = &bttv_tvnorms[norm];
1110
1111 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1112 sizeof (tvnorm->cropcap))) {
1113 bttv_crop_reset(&btv->crop[0], norm);
1114 btv->crop[1] = btv->crop[0]; /* current = default */
1115
1116 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1117 btv->crop_start = tvnorm->cropcap.bounds.top
1118 + tvnorm->cropcap.bounds.height;
1119 }
1120 }
1121
1122 btv->tvnorm = norm;
1123
1124 btwrite(tvnorm->adelay, BT848_ADELAY);
1125 btwrite(tvnorm->bdelay, BT848_BDELAY);
1126 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1127 BT848_IFORM);
1128 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1129 btwrite(1, BT848_VBI_PACK_DEL);
1130 bt848A_set_timing(btv);
1131
1132 switch (btv->c.type) {
1133 case BTTV_BOARD_VOODOOTV_FM:
1134 case BTTV_BOARD_VOODOOTV_200:
1135 bttv_tda9880_setnorm(btv, gpio_read());
1136 break;
1137 }
1138 id = tvnorm->v4l2_id;
1139 bttv_call_all(btv, video, s_std, id);
1140
1141 return 0;
1142 }
1143
1144 /* Call with btv->lock down. */
1145 static void
set_input(struct bttv * btv,unsigned int input,unsigned int norm)1146 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1147 {
1148 unsigned long flags;
1149
1150 btv->input = input;
1151 if (irq_iswitch) {
1152 spin_lock_irqsave(&btv->s_lock,flags);
1153 if (btv->curr.frame_irq) {
1154 /* active capture -> delayed input switch */
1155 btv->new_input = input;
1156 } else {
1157 video_mux(btv,input);
1158 }
1159 spin_unlock_irqrestore(&btv->s_lock,flags);
1160 } else {
1161 video_mux(btv,input);
1162 }
1163 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1164 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1165 audio_input(btv, btv->audio_input);
1166 set_tvnorm(btv, norm);
1167 }
1168
init_irqreg(struct bttv * btv)1169 void init_irqreg(struct bttv *btv)
1170 {
1171 /* clear status */
1172 btwrite(0xfffffUL, BT848_INT_STAT);
1173
1174 if (bttv_tvcards[btv->c.type].no_video) {
1175 /* i2c only */
1176 btwrite(BT848_INT_I2CDONE,
1177 BT848_INT_MASK);
1178 } else {
1179 /* full video */
1180 btwrite((btv->triton1) |
1181 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1182 BT848_INT_SCERR |
1183 (fdsr ? BT848_INT_FDSR : 0) |
1184 BT848_INT_RISCI | BT848_INT_OCERR |
1185 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1186 BT848_INT_I2CDONE,
1187 BT848_INT_MASK);
1188 }
1189 }
1190
init_bt848(struct bttv * btv)1191 static void init_bt848(struct bttv *btv)
1192 {
1193 if (bttv_tvcards[btv->c.type].no_video) {
1194 /* very basic init only */
1195 init_irqreg(btv);
1196 return;
1197 }
1198
1199 btwrite(0x00, BT848_CAP_CTL);
1200 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1201 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1202
1203 /* set planar and packed mode trigger points and */
1204 /* set rising edge of inverted GPINTR pin as irq trigger */
1205 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1206 BT848_GPIO_DMA_CTL_PLTP1_16|
1207 BT848_GPIO_DMA_CTL_PLTP23_16|
1208 BT848_GPIO_DMA_CTL_GPINTC|
1209 BT848_GPIO_DMA_CTL_GPINTI,
1210 BT848_GPIO_DMA_CTL);
1211
1212 btwrite(0x20, BT848_E_VSCALE_HI);
1213 btwrite(0x20, BT848_O_VSCALE_HI);
1214
1215 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
1216
1217 /* interrupt */
1218 init_irqreg(btv);
1219 }
1220
bttv_reinit_bt848(struct bttv * btv)1221 static void bttv_reinit_bt848(struct bttv *btv)
1222 {
1223 unsigned long flags;
1224
1225 if (bttv_verbose)
1226 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1227 spin_lock_irqsave(&btv->s_lock,flags);
1228 btv->errors=0;
1229 bttv_set_dma(btv,0);
1230 spin_unlock_irqrestore(&btv->s_lock,flags);
1231
1232 init_bt848(btv);
1233 btv->pll.pll_current = -1;
1234 set_input(btv, btv->input, btv->tvnorm);
1235 }
1236
bttv_s_ctrl(struct v4l2_ctrl * c)1237 static int bttv_s_ctrl(struct v4l2_ctrl *c)
1238 {
1239 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1240 int val;
1241
1242 switch (c->id) {
1243 case V4L2_CID_BRIGHTNESS:
1244 bt848_bright(btv, c->val);
1245 break;
1246 case V4L2_CID_HUE:
1247 bt848_hue(btv, c->val);
1248 break;
1249 case V4L2_CID_CONTRAST:
1250 bt848_contrast(btv, c->val);
1251 break;
1252 case V4L2_CID_SATURATION:
1253 bt848_sat(btv, c->val);
1254 break;
1255 case V4L2_CID_COLOR_KILLER:
1256 if (c->val) {
1257 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1258 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1259 } else {
1260 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1261 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1262 }
1263 break;
1264 case V4L2_CID_AUDIO_MUTE:
1265 audio_mute(btv, c->val);
1266 btv->mute = c->val;
1267 break;
1268 case V4L2_CID_AUDIO_VOLUME:
1269 btv->volume_gpio(btv, c->val);
1270 break;
1271
1272 case V4L2_CID_CHROMA_AGC:
1273 val = c->val ? BT848_SCLOOP_CAGC : 0;
1274 btwrite(val, BT848_E_SCLOOP);
1275 btwrite(val, BT848_O_SCLOOP);
1276 break;
1277 case V4L2_CID_PRIVATE_COMBFILTER:
1278 btv->opt_combfilter = c->val;
1279 break;
1280 case V4L2_CID_PRIVATE_LUMAFILTER:
1281 if (c->val) {
1282 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1283 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1284 } else {
1285 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1286 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1287 }
1288 break;
1289 case V4L2_CID_PRIVATE_AUTOMUTE:
1290 btv->opt_automute = c->val;
1291 break;
1292 case V4L2_CID_PRIVATE_AGC_CRUSH:
1293 btwrite(BT848_ADC_RESERVED |
1294 (c->val ? BT848_ADC_CRUSH : 0),
1295 BT848_ADC);
1296 break;
1297 case V4L2_CID_PRIVATE_VCR_HACK:
1298 btv->opt_vcr_hack = c->val;
1299 break;
1300 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1301 btwrite(c->val, BT848_WC_UP);
1302 break;
1303 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1304 btwrite(c->val, BT848_WC_DOWN);
1305 break;
1306 case V4L2_CID_PRIVATE_UV_RATIO:
1307 btv->opt_uv_ratio = c->val;
1308 bt848_sat(btv, btv->saturation);
1309 break;
1310 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1311 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1312 break;
1313 case V4L2_CID_PRIVATE_CORING:
1314 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1315 break;
1316 default:
1317 return -EINVAL;
1318 }
1319 return 0;
1320 }
1321
1322 /* ----------------------------------------------------------------------- */
1323
1324 static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1325 .s_ctrl = bttv_s_ctrl,
1326 };
1327
1328 static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1329 .ops = &bttv_ctrl_ops,
1330 .id = V4L2_CID_PRIVATE_COMBFILTER,
1331 .name = "Comb Filter",
1332 .type = V4L2_CTRL_TYPE_BOOLEAN,
1333 .min = 0,
1334 .max = 1,
1335 .step = 1,
1336 .def = 1,
1337 };
1338
1339 static struct v4l2_ctrl_config bttv_ctrl_automute = {
1340 .ops = &bttv_ctrl_ops,
1341 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1342 .name = "Auto Mute",
1343 .type = V4L2_CTRL_TYPE_BOOLEAN,
1344 .min = 0,
1345 .max = 1,
1346 .step = 1,
1347 .def = 1,
1348 };
1349
1350 static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1351 .ops = &bttv_ctrl_ops,
1352 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1353 .name = "Luma Decimation Filter",
1354 .type = V4L2_CTRL_TYPE_BOOLEAN,
1355 .min = 0,
1356 .max = 1,
1357 .step = 1,
1358 .def = 1,
1359 };
1360
1361 static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1362 .ops = &bttv_ctrl_ops,
1363 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1364 .name = "AGC Crush",
1365 .type = V4L2_CTRL_TYPE_BOOLEAN,
1366 .min = 0,
1367 .max = 1,
1368 .step = 1,
1369 .def = 1,
1370 };
1371
1372 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1373 .ops = &bttv_ctrl_ops,
1374 .id = V4L2_CID_PRIVATE_VCR_HACK,
1375 .name = "VCR Hack",
1376 .type = V4L2_CTRL_TYPE_BOOLEAN,
1377 .min = 0,
1378 .max = 1,
1379 .step = 1,
1380 .def = 1,
1381 };
1382
1383 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1384 .ops = &bttv_ctrl_ops,
1385 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1386 .name = "Whitecrush Lower",
1387 .type = V4L2_CTRL_TYPE_INTEGER,
1388 .min = 0,
1389 .max = 255,
1390 .step = 1,
1391 .def = 0x7f,
1392 };
1393
1394 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1395 .ops = &bttv_ctrl_ops,
1396 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1397 .name = "Whitecrush Upper",
1398 .type = V4L2_CTRL_TYPE_INTEGER,
1399 .min = 0,
1400 .max = 255,
1401 .step = 1,
1402 .def = 0xcf,
1403 };
1404
1405 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1406 .ops = &bttv_ctrl_ops,
1407 .id = V4L2_CID_PRIVATE_UV_RATIO,
1408 .name = "UV Ratio",
1409 .type = V4L2_CTRL_TYPE_INTEGER,
1410 .min = 0,
1411 .max = 100,
1412 .step = 1,
1413 .def = 50,
1414 };
1415
1416 static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1417 .ops = &bttv_ctrl_ops,
1418 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1419 .name = "Full Luma Range",
1420 .type = V4L2_CTRL_TYPE_BOOLEAN,
1421 .min = 0,
1422 .max = 1,
1423 .step = 1,
1424 };
1425
1426 static struct v4l2_ctrl_config bttv_ctrl_coring = {
1427 .ops = &bttv_ctrl_ops,
1428 .id = V4L2_CID_PRIVATE_CORING,
1429 .name = "Coring",
1430 .type = V4L2_CTRL_TYPE_INTEGER,
1431 .min = 0,
1432 .max = 3,
1433 .step = 1,
1434 };
1435
1436
1437 /* ----------------------------------------------------------------------- */
1438
bttv_gpio_tracking(struct bttv * btv,char * comment)1439 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1440 {
1441 unsigned int outbits, data;
1442 outbits = btread(BT848_GPIO_OUT_EN);
1443 data = btread(BT848_GPIO_DATA);
1444 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1445 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1446 }
1447
1448 static const struct bttv_format*
format_by_fourcc(int fourcc)1449 format_by_fourcc(int fourcc)
1450 {
1451 unsigned int i;
1452
1453 for (i = 0; i < FORMATS; i++) {
1454 if (-1 == formats[i].fourcc)
1455 continue;
1456 if (formats[i].fourcc == fourcc)
1457 return formats+i;
1458 }
1459 return NULL;
1460 }
1461
1462 /* ----------------------------------------------------------------------- */
1463 /* video4linux (1) interface */
1464
queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])1465 static int queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
1466 unsigned int *num_planes, unsigned int sizes[],
1467 struct device *alloc_devs[])
1468 {
1469 struct bttv *btv = vb2_get_drv_priv(q);
1470 unsigned int size = btv->fmt->depth * btv->width * btv->height >> 3;
1471
1472 if (*num_planes)
1473 return sizes[0] < size ? -EINVAL : 0;
1474 *num_planes = 1;
1475 sizes[0] = size;
1476
1477 return 0;
1478 }
1479
buf_queue(struct vb2_buffer * vb)1480 static void buf_queue(struct vb2_buffer *vb)
1481 {
1482 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1483 struct vb2_queue *vq = vb->vb2_queue;
1484 struct bttv *btv = vb2_get_drv_priv(vq);
1485 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1486 unsigned long flags;
1487
1488 spin_lock_irqsave(&btv->s_lock, flags);
1489 if (list_empty(&btv->capture)) {
1490 btv->loop_irq = BT848_RISC_VIDEO;
1491 if (vb2_is_streaming(&btv->vbiq))
1492 btv->loop_irq |= BT848_RISC_VBI;
1493 bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_ODD |
1494 BT848_CAP_CTL_CAPTURE_EVEN);
1495 }
1496 list_add_tail(&buf->list, &btv->capture);
1497 spin_unlock_irqrestore(&btv->s_lock, flags);
1498 }
1499
buf_prepare(struct vb2_buffer * vb)1500 static int buf_prepare(struct vb2_buffer *vb)
1501 {
1502 struct vb2_queue *vq = vb->vb2_queue;
1503 struct bttv *btv = vb2_get_drv_priv(vq);
1504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1505 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1506 unsigned int size = (btv->fmt->depth * btv->width * btv->height) >> 3;
1507
1508 if (vb2_plane_size(vb, 0) < size)
1509 return -EINVAL;
1510 vb2_set_plane_payload(vb, 0, size);
1511
1512 if (btv->field != V4L2_FIELD_ALTERNATE) {
1513 buf->vbuf.field = btv->field;
1514 } else if (btv->field_last == V4L2_FIELD_TOP) {
1515 buf->vbuf.field = V4L2_FIELD_BOTTOM;
1516 btv->field_last = V4L2_FIELD_BOTTOM;
1517 } else {
1518 buf->vbuf.field = V4L2_FIELD_TOP;
1519 btv->field_last = V4L2_FIELD_TOP;
1520 }
1521
1522 /* Allocate memory for risc struct and create the risc program. */
1523 return bttv_buffer_risc(btv, buf);
1524 }
1525
buf_cleanup(struct vb2_buffer * vb)1526 static void buf_cleanup(struct vb2_buffer *vb)
1527 {
1528 struct vb2_queue *vq = vb->vb2_queue;
1529 struct bttv *btv = vb2_get_drv_priv(vq);
1530 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1531 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1532
1533 btcx_riscmem_free(btv->c.pci, &buf->top);
1534 btcx_riscmem_free(btv->c.pci, &buf->bottom);
1535 }
1536
start_streaming(struct vb2_queue * q,unsigned int count)1537 static int start_streaming(struct vb2_queue *q, unsigned int count)
1538 {
1539 int seqnr = 0;
1540 struct bttv_buffer *buf;
1541 struct bttv *btv = vb2_get_drv_priv(q);
1542
1543 if (!check_alloc_btres_lock(btv, RESOURCE_VIDEO_STREAM)) {
1544 if (btv->field_count)
1545 seqnr++;
1546 while (!list_empty(&btv->capture)) {
1547 buf = list_entry(btv->capture.next,
1548 struct bttv_buffer, list);
1549 list_del(&buf->list);
1550 buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
1551 vb2_buffer_done(&buf->vbuf.vb2_buf,
1552 VB2_BUF_STATE_QUEUED);
1553 }
1554 return -EBUSY;
1555 }
1556 if (!vb2_is_streaming(&btv->vbiq)) {
1557 init_irqreg(btv);
1558 btv->field_count = 0;
1559 }
1560 btv->framedrop = 0;
1561
1562 return 0;
1563 }
1564
stop_streaming(struct vb2_queue * q)1565 static void stop_streaming(struct vb2_queue *q)
1566 {
1567 unsigned long flags;
1568 struct bttv *btv = vb2_get_drv_priv(q);
1569
1570 vb2_wait_for_all_buffers(q);
1571 spin_lock_irqsave(&btv->s_lock, flags);
1572 free_btres_lock(btv, RESOURCE_VIDEO_STREAM);
1573 if (!vb2_is_streaming(&btv->vbiq)) {
1574 /* stop field counter */
1575 btand(~BT848_INT_VSYNC, BT848_INT_MASK);
1576 }
1577 spin_unlock_irqrestore(&btv->s_lock, flags);
1578 }
1579
1580 static const struct vb2_ops bttv_video_qops = {
1581 .queue_setup = queue_setup,
1582 .buf_queue = buf_queue,
1583 .buf_prepare = buf_prepare,
1584 .buf_cleanup = buf_cleanup,
1585 .start_streaming = start_streaming,
1586 .stop_streaming = stop_streaming,
1587 .wait_prepare = vb2_ops_wait_prepare,
1588 .wait_finish = vb2_ops_wait_finish,
1589 };
1590
radio_enable(struct bttv * btv)1591 static void radio_enable(struct bttv *btv)
1592 {
1593 /* Switch to the radio tuner */
1594 if (!btv->has_radio_tuner) {
1595 btv->has_radio_tuner = 1;
1596 bttv_call_all(btv, tuner, s_radio);
1597 btv->audio_input = TVAUDIO_INPUT_RADIO;
1598 audio_input(btv, btv->audio_input);
1599 }
1600 }
1601
bttv_s_std(struct file * file,void * priv,v4l2_std_id id)1602 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
1603 {
1604 struct bttv *btv = video_drvdata(file);
1605 unsigned int i;
1606
1607 for (i = 0; i < BTTV_TVNORMS; i++)
1608 if (id & bttv_tvnorms[i].v4l2_id)
1609 break;
1610 if (i == BTTV_TVNORMS)
1611 return -EINVAL;
1612 btv->std = id;
1613 set_tvnorm(btv, i);
1614 return 0;
1615 }
1616
bttv_g_std(struct file * file,void * priv,v4l2_std_id * id)1617 static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1618 {
1619 struct bttv *btv = video_drvdata(file);
1620
1621 *id = btv->std;
1622 return 0;
1623 }
1624
bttv_querystd(struct file * file,void * f,v4l2_std_id * id)1625 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1626 {
1627 struct bttv *btv = video_drvdata(file);
1628
1629 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1630 *id &= V4L2_STD_625_50;
1631 else
1632 *id &= V4L2_STD_525_60;
1633 return 0;
1634 }
1635
bttv_enum_input(struct file * file,void * priv,struct v4l2_input * i)1636 static int bttv_enum_input(struct file *file, void *priv,
1637 struct v4l2_input *i)
1638 {
1639 struct bttv *btv = video_drvdata(file);
1640
1641 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1642 return -EINVAL;
1643
1644 i->type = V4L2_INPUT_TYPE_CAMERA;
1645 i->audioset = 0;
1646
1647 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1648 sprintf(i->name, "Television");
1649 i->type = V4L2_INPUT_TYPE_TUNER;
1650 i->tuner = 0;
1651 } else if (i->index == btv->svhs) {
1652 sprintf(i->name, "S-Video");
1653 } else {
1654 sprintf(i->name, "Composite%d", i->index);
1655 }
1656
1657 if (i->index == btv->input) {
1658 __u32 dstatus = btread(BT848_DSTATUS);
1659 if (0 == (dstatus & BT848_DSTATUS_PRES))
1660 i->status |= V4L2_IN_ST_NO_SIGNAL;
1661 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1662 i->status |= V4L2_IN_ST_NO_H_LOCK;
1663 }
1664
1665 i->std = BTTV_NORMS;
1666 return 0;
1667 }
1668
bttv_g_input(struct file * file,void * priv,unsigned int * i)1669 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1670 {
1671 struct bttv *btv = video_drvdata(file);
1672
1673 *i = btv->input;
1674
1675 return 0;
1676 }
1677
bttv_s_input(struct file * file,void * priv,unsigned int i)1678 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1679 {
1680 struct bttv *btv = video_drvdata(file);
1681
1682 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1683 return -EINVAL;
1684
1685 set_input(btv, i, btv->tvnorm);
1686 return 0;
1687 }
1688
bttv_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)1689 static int bttv_s_tuner(struct file *file, void *priv,
1690 const struct v4l2_tuner *t)
1691 {
1692 struct bttv *btv = video_drvdata(file);
1693
1694 if (t->index)
1695 return -EINVAL;
1696
1697 bttv_call_all(btv, tuner, s_tuner, t);
1698
1699 if (btv->audio_mode_gpio) {
1700 struct v4l2_tuner copy = *t;
1701
1702 btv->audio_mode_gpio(btv, ©, 1);
1703 }
1704 return 0;
1705 }
1706
bttv_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)1707 static int bttv_g_frequency(struct file *file, void *priv,
1708 struct v4l2_frequency *f)
1709 {
1710 struct bttv *btv = video_drvdata(file);
1711
1712 if (f->tuner)
1713 return -EINVAL;
1714
1715 if (f->type == V4L2_TUNER_RADIO)
1716 radio_enable(btv);
1717 f->frequency = f->type == V4L2_TUNER_RADIO ?
1718 btv->radio_freq : btv->tv_freq;
1719
1720 return 0;
1721 }
1722
bttv_set_frequency(struct bttv * btv,const struct v4l2_frequency * f)1723 static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1724 {
1725 struct v4l2_frequency new_freq = *f;
1726
1727 bttv_call_all(btv, tuner, s_frequency, f);
1728 /* s_frequency may clamp the frequency, so get the actual
1729 frequency before assigning radio/tv_freq. */
1730 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1731 if (new_freq.type == V4L2_TUNER_RADIO) {
1732 radio_enable(btv);
1733 btv->radio_freq = new_freq.frequency;
1734 if (btv->has_tea575x) {
1735 btv->tea.freq = btv->radio_freq;
1736 snd_tea575x_set_freq(&btv->tea);
1737 }
1738 } else {
1739 btv->tv_freq = new_freq.frequency;
1740 }
1741 }
1742
bttv_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)1743 static int bttv_s_frequency(struct file *file, void *priv,
1744 const struct v4l2_frequency *f)
1745 {
1746 struct bttv *btv = video_drvdata(file);
1747
1748 if (f->tuner)
1749 return -EINVAL;
1750
1751 bttv_set_frequency(btv, f);
1752 return 0;
1753 }
1754
bttv_log_status(struct file * file,void * f)1755 static int bttv_log_status(struct file *file, void *f)
1756 {
1757 struct video_device *vdev = video_devdata(file);
1758 struct bttv *btv = video_drvdata(file);
1759
1760 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1761 bttv_call_all(btv, core, log_status);
1762 return 0;
1763 }
1764
1765 #ifdef CONFIG_VIDEO_ADV_DEBUG
bttv_g_register(struct file * file,void * f,struct v4l2_dbg_register * reg)1766 static int bttv_g_register(struct file *file, void *f,
1767 struct v4l2_dbg_register *reg)
1768 {
1769 struct bttv *btv = video_drvdata(file);
1770
1771 /* bt848 has a 12-bit register space */
1772 reg->reg &= 0xfff;
1773 reg->val = btread(reg->reg);
1774 reg->size = 1;
1775
1776 return 0;
1777 }
1778
bttv_s_register(struct file * file,void * f,const struct v4l2_dbg_register * reg)1779 static int bttv_s_register(struct file *file, void *f,
1780 const struct v4l2_dbg_register *reg)
1781 {
1782 struct bttv *btv = video_drvdata(file);
1783
1784 /* bt848 has a 12-bit register space */
1785 btwrite(reg->val, reg->reg & 0xfff);
1786
1787 return 0;
1788 }
1789 #endif
1790
1791 /* Given cropping boundaries b and the scaled width and height of a
1792 single field or frame, which must not exceed hardware limits, this
1793 function adjusts the cropping parameters c. */
1794 static void
bttv_crop_adjust(struct bttv_crop * c,const struct v4l2_rect * b,__s32 width,__s32 height,enum v4l2_field field)1795 bttv_crop_adjust (struct bttv_crop * c,
1796 const struct v4l2_rect * b,
1797 __s32 width,
1798 __s32 height,
1799 enum v4l2_field field)
1800 {
1801 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1802 __s32 max_left;
1803 __s32 max_top;
1804
1805 if (width < c->min_scaled_width) {
1806 /* Max. hor. scale factor 16:1. */
1807 c->rect.width = width * 16;
1808 } else if (width > c->max_scaled_width) {
1809 /* Min. hor. scale factor 1:1. */
1810 c->rect.width = width;
1811
1812 max_left = b->left + b->width - width;
1813 max_left = min(max_left, (__s32) MAX_HDELAY);
1814 if (c->rect.left > max_left)
1815 c->rect.left = max_left;
1816 }
1817
1818 if (height < c->min_scaled_height) {
1819 /* Max. vert. scale factor 16:1, single fields 8:1. */
1820 c->rect.height = height * 16;
1821 } else if (frame_height > c->max_scaled_height) {
1822 /* Min. vert. scale factor 1:1.
1823 Top and height count field lines times two. */
1824 c->rect.height = (frame_height + 1) & ~1;
1825
1826 max_top = b->top + b->height - c->rect.height;
1827 if (c->rect.top > max_top)
1828 c->rect.top = max_top;
1829 }
1830
1831 bttv_crop_calc_limits(c);
1832 }
1833
1834 /* Returns an error if scaling to a frame or single field with the given
1835 width and height is not possible with the current cropping parameters
1836 and width aligned according to width_mask. If adjust_size is TRUE the
1837 function may adjust the width and/or height instead, rounding width
1838 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1839 also adjust the current cropping parameters to get closer to the
1840 desired image size. */
1841 static int
limit_scaled_size_lock(struct bttv * btv,__s32 * width,__s32 * height,enum v4l2_field field,unsigned int width_mask,unsigned int width_bias,int adjust_size,int adjust_crop)1842 limit_scaled_size_lock(struct bttv *btv, __s32 *width, __s32 *height,
1843 enum v4l2_field field, unsigned int width_mask,
1844 unsigned int width_bias, int adjust_size,
1845 int adjust_crop)
1846 {
1847 const struct v4l2_rect *b;
1848 struct bttv_crop *c;
1849 __s32 min_width;
1850 __s32 min_height;
1851 __s32 max_width;
1852 __s32 max_height;
1853 int rc;
1854
1855 WARN_ON((int)width_mask >= 0 ||
1856 width_bias >= (unsigned int)(-width_mask));
1857
1858 /* Make sure tvnorm, vbi_end and the current cropping parameters
1859 remain consistent until we're done. */
1860
1861 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
1862
1863 /* Do crop - use current, don't - use default parameters. */
1864 c = &btv->crop[!!btv->do_crop];
1865
1866 if (btv->do_crop
1867 && adjust_size
1868 && adjust_crop
1869 && !locked_btres(btv, VIDEO_RESOURCES)) {
1870 min_width = 48;
1871 min_height = 32;
1872
1873 /* We cannot scale up. When the scaled image is larger
1874 than crop.rect we adjust the crop.rect as required
1875 by the V4L2 spec, hence cropcap.bounds are our limit. */
1876 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
1877 max_height = b->height;
1878
1879 /* We cannot capture the same line as video and VBI data.
1880 Note btv->vbi_end is really a minimum, see
1881 bttv_vbi_try_fmt(). */
1882 if (btv->vbi_end > b->top) {
1883 max_height -= btv->vbi_end - b->top;
1884 rc = -EBUSY;
1885 if (min_height > max_height)
1886 goto fail;
1887 }
1888 } else {
1889 rc = -EBUSY;
1890 if (btv->vbi_end > c->rect.top)
1891 goto fail;
1892
1893 min_width = c->min_scaled_width;
1894 min_height = c->min_scaled_height;
1895 max_width = c->max_scaled_width;
1896 max_height = c->max_scaled_height;
1897
1898 adjust_crop = 0;
1899 }
1900
1901 min_width = (min_width - width_mask - 1) & width_mask;
1902 max_width = max_width & width_mask;
1903
1904 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
1905 /* Min. scale factor is 1:1. */
1906 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
1907
1908 if (adjust_size) {
1909 *width = clamp(*width, min_width, max_width);
1910 *height = clamp(*height, min_height, max_height);
1911
1912 /* Round after clamping to avoid overflow. */
1913 *width = (*width + width_bias) & width_mask;
1914
1915 if (adjust_crop) {
1916 bttv_crop_adjust(c, b, *width, *height, field);
1917
1918 if (btv->vbi_end > c->rect.top) {
1919 /* Move the crop window out of the way. */
1920 c->rect.top = btv->vbi_end;
1921 }
1922 }
1923 } else {
1924 rc = -EINVAL;
1925 if (*width < min_width ||
1926 *height < min_height ||
1927 *width > max_width ||
1928 *height > max_height ||
1929 0 != (*width & ~width_mask))
1930 goto fail;
1931 }
1932
1933 rc = 0; /* success */
1934
1935 fail:
1936
1937 return rc;
1938 }
1939
bttv_switch_type(struct bttv * btv,enum v4l2_buf_type type)1940 static int bttv_switch_type(struct bttv *btv, enum v4l2_buf_type type)
1941 {
1942 int res;
1943 struct vb2_queue *q;
1944
1945 switch (type) {
1946 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1947 q = &btv->capq;
1948 res = RESOURCE_VIDEO_STREAM;
1949 break;
1950 case V4L2_BUF_TYPE_VBI_CAPTURE:
1951 q = &btv->vbiq;
1952 res = RESOURCE_VBI;
1953 break;
1954 default:
1955 WARN_ON(1);
1956 return -EINVAL;
1957 }
1958
1959 if (check_btres(btv, res))
1960 return -EBUSY;
1961 if (vb2_is_busy(q))
1962 return -EBUSY;
1963 btv->type = type;
1964
1965 return 0;
1966 }
1967
1968 static void
pix_format_set_size(struct v4l2_pix_format * f,const struct bttv_format * fmt,unsigned int width,unsigned int height)1969 pix_format_set_size (struct v4l2_pix_format * f,
1970 const struct bttv_format * fmt,
1971 unsigned int width,
1972 unsigned int height)
1973 {
1974 f->width = width;
1975 f->height = height;
1976
1977 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
1978 f->bytesperline = width; /* Y plane */
1979 f->sizeimage = (width * height * fmt->depth) >> 3;
1980 } else {
1981 f->bytesperline = (width * fmt->depth) >> 3;
1982 f->sizeimage = height * f->bytesperline;
1983 }
1984 }
1985
bttv_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1986 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
1987 struct v4l2_format *f)
1988 {
1989 struct bttv *btv = video_drvdata(file);
1990
1991 pix_format_set_size(&f->fmt.pix, btv->fmt, btv->width, btv->height);
1992 f->fmt.pix.field = btv->field;
1993 f->fmt.pix.pixelformat = btv->fmt->fourcc;
1994 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1995
1996 return 0;
1997 }
1998
bttv_get_width_mask_vid_cap(const struct bttv_format * fmt,unsigned int * width_mask,unsigned int * width_bias)1999 static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
2000 unsigned int *width_mask,
2001 unsigned int *width_bias)
2002 {
2003 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2004 *width_mask = ~15; /* width must be a multiple of 16 pixels */
2005 *width_bias = 8; /* nearest */
2006 } else {
2007 *width_mask = ~3; /* width must be a multiple of 4 pixels */
2008 *width_bias = 2; /* nearest */
2009 }
2010 }
2011
bttv_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)2012 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2013 struct v4l2_format *f)
2014 {
2015 const struct bttv_format *fmt;
2016 struct bttv *btv = video_drvdata(file);
2017 enum v4l2_field field;
2018 __s32 width, height;
2019 __s32 height2;
2020 unsigned int width_mask, width_bias;
2021 int rc;
2022
2023 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2024 if (NULL == fmt)
2025 return -EINVAL;
2026
2027 field = f->fmt.pix.field;
2028
2029 switch (field) {
2030 case V4L2_FIELD_TOP:
2031 case V4L2_FIELD_BOTTOM:
2032 case V4L2_FIELD_ALTERNATE:
2033 case V4L2_FIELD_INTERLACED:
2034 break;
2035 case V4L2_FIELD_SEQ_BT:
2036 case V4L2_FIELD_SEQ_TB:
2037 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2038 field = V4L2_FIELD_SEQ_TB;
2039 break;
2040 }
2041 fallthrough;
2042 default: /* FIELD_ANY case */
2043 height2 = btv->crop[!!btv->do_crop].rect.height >> 1;
2044 field = (f->fmt.pix.height > height2)
2045 ? V4L2_FIELD_INTERLACED
2046 : V4L2_FIELD_BOTTOM;
2047 break;
2048 }
2049
2050 width = f->fmt.pix.width;
2051 height = f->fmt.pix.height;
2052
2053 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2054 rc = limit_scaled_size_lock(btv, &width, &height, field, width_mask,
2055 width_bias, 1, 0);
2056 if (0 != rc)
2057 return rc;
2058
2059 /* update data for the application */
2060 f->fmt.pix.field = field;
2061 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2062 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2063
2064 return 0;
2065 }
2066
bttv_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)2067 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2068 struct v4l2_format *f)
2069 {
2070 int retval;
2071 const struct bttv_format *fmt;
2072 struct bttv *btv = video_drvdata(file);
2073 __s32 width, height;
2074 unsigned int width_mask, width_bias;
2075 enum v4l2_field field;
2076
2077 retval = bttv_switch_type(btv, f->type);
2078 if (0 != retval)
2079 return retval;
2080
2081 retval = bttv_try_fmt_vid_cap(file, priv, f);
2082 if (0 != retval)
2083 return retval;
2084
2085 width = f->fmt.pix.width;
2086 height = f->fmt.pix.height;
2087 field = f->fmt.pix.field;
2088
2089 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2090 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2091 retval = limit_scaled_size_lock(btv, &width, &height, f->fmt.pix.field,
2092 width_mask, width_bias, 1, 1);
2093 if (0 != retval)
2094 return retval;
2095
2096 f->fmt.pix.field = field;
2097
2098 /* update our state information */
2099 btv->fmt = fmt;
2100 btv->width = f->fmt.pix.width;
2101 btv->height = f->fmt.pix.height;
2102 btv->field = f->fmt.pix.field;
2103 /*
2104 * When field is V4L2_FIELD_ALTERNATE, buffers will be either
2105 * V4L2_FIELD_TOP or V4L2_FIELD_BOTTOM depending on the value of
2106 * field_last. Initialize field_last to V4L2_FIELD_BOTTOM so that
2107 * streaming starts with a V4L2_FIELD_TOP buffer.
2108 */
2109 btv->field_last = V4L2_FIELD_BOTTOM;
2110
2111 return 0;
2112 }
2113
bttv_querycap(struct file * file,void * priv,struct v4l2_capability * cap)2114 static int bttv_querycap(struct file *file, void *priv,
2115 struct v4l2_capability *cap)
2116 {
2117 struct bttv *btv = video_drvdata(file);
2118
2119 if (0 == v4l2)
2120 return -EINVAL;
2121
2122 strscpy(cap->driver, "bttv", sizeof(cap->driver));
2123 strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2124 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2125 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
2126 if (video_is_registered(&btv->vbi_dev))
2127 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2128 if (video_is_registered(&btv->radio_dev)) {
2129 cap->capabilities |= V4L2_CAP_RADIO;
2130 if (btv->has_tea575x)
2131 cap->capabilities |= V4L2_CAP_HW_FREQ_SEEK;
2132 }
2133
2134 /*
2135 * No need to lock here: those vars are initialized during board
2136 * probe and remains untouched during the rest of the driver lifecycle
2137 */
2138 if (btv->has_saa6588)
2139 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2140 if (btv->tuner_type != TUNER_ABSENT)
2141 cap->capabilities |= V4L2_CAP_TUNER;
2142 return 0;
2143 }
2144
bttv_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)2145 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2146 struct v4l2_fmtdesc *f)
2147 {
2148 int index = -1, i;
2149
2150 for (i = 0; i < FORMATS; i++) {
2151 if (formats[i].fourcc != -1)
2152 index++;
2153 if ((unsigned int)index == f->index)
2154 break;
2155 }
2156 if (FORMATS == i)
2157 return -EINVAL;
2158
2159 f->pixelformat = formats[i].fourcc;
2160
2161 return 0;
2162 }
2163
bttv_g_parm(struct file * file,void * f,struct v4l2_streamparm * parm)2164 static int bttv_g_parm(struct file *file, void *f,
2165 struct v4l2_streamparm *parm)
2166 {
2167 struct bttv *btv = video_drvdata(file);
2168
2169 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2170 return -EINVAL;
2171 parm->parm.capture.readbuffers = gbuffers;
2172 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2173 &parm->parm.capture.timeperframe);
2174
2175 return 0;
2176 }
2177
bttv_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)2178 static int bttv_g_tuner(struct file *file, void *priv,
2179 struct v4l2_tuner *t)
2180 {
2181 struct bttv *btv = video_drvdata(file);
2182
2183 if (0 != t->index)
2184 return -EINVAL;
2185
2186 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2187 t->capability = V4L2_TUNER_CAP_NORM;
2188 bttv_call_all(btv, tuner, g_tuner, t);
2189 strscpy(t->name, "Television", sizeof(t->name));
2190 t->type = V4L2_TUNER_ANALOG_TV;
2191 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2192 t->signal = 0xffff;
2193
2194 if (btv->audio_mode_gpio)
2195 btv->audio_mode_gpio(btv, t, 0);
2196
2197 return 0;
2198 }
2199
bttv_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)2200 static int bttv_g_pixelaspect(struct file *file, void *priv,
2201 int type, struct v4l2_fract *f)
2202 {
2203 struct bttv *btv = video_drvdata(file);
2204
2205 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2206 return -EINVAL;
2207
2208 /* defrect and bounds are set via g_selection */
2209 *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
2210 return 0;
2211 }
2212
bttv_g_selection(struct file * file,void * f,struct v4l2_selection * sel)2213 static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
2214 {
2215 struct bttv *btv = video_drvdata(file);
2216
2217 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2218 return -EINVAL;
2219
2220 switch (sel->target) {
2221 case V4L2_SEL_TGT_CROP:
2222 sel->r = btv->crop[!!btv->do_crop].rect;
2223 break;
2224 case V4L2_SEL_TGT_CROP_DEFAULT:
2225 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
2226 break;
2227 case V4L2_SEL_TGT_CROP_BOUNDS:
2228 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2229 break;
2230 default:
2231 return -EINVAL;
2232 }
2233
2234 return 0;
2235 }
2236
bttv_s_selection(struct file * file,void * f,struct v4l2_selection * sel)2237 static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
2238 {
2239 struct bttv *btv = video_drvdata(file);
2240 const struct v4l2_rect *b;
2241 int retval;
2242 struct bttv_crop c;
2243 __s32 b_left;
2244 __s32 b_top;
2245 __s32 b_right;
2246 __s32 b_bottom;
2247
2248 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2249 return -EINVAL;
2250
2251 if (sel->target != V4L2_SEL_TGT_CROP)
2252 return -EINVAL;
2253
2254 /* Make sure tvnorm, vbi_end and the current cropping
2255 parameters remain consistent until we're done. Note
2256 read() may change vbi_end in check_alloc_btres_lock(). */
2257 retval = -EBUSY;
2258
2259 if (locked_btres(btv, VIDEO_RESOURCES))
2260 return retval;
2261
2262 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2263
2264 b_left = b->left;
2265 b_right = b_left + b->width;
2266 b_bottom = b->top + b->height;
2267
2268 b_top = max(b->top, btv->vbi_end);
2269 if (b_top + 32 >= b_bottom) {
2270 return retval;
2271 }
2272
2273 /* Min. scaled size 48 x 32. */
2274 c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
2275 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2276
2277 c.rect.width = clamp_t(s32, sel->r.width,
2278 48, b_right - c.rect.left);
2279
2280 c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
2281 /* Top and height must be a multiple of two. */
2282 c.rect.top = (c.rect.top + 1) & ~1;
2283
2284 c.rect.height = clamp_t(s32, sel->r.height,
2285 32, b_bottom - c.rect.top);
2286 c.rect.height = (c.rect.height + 1) & ~1;
2287
2288 bttv_crop_calc_limits(&c);
2289
2290 sel->r = c.rect;
2291
2292 btv->crop[1] = c;
2293
2294 btv->do_crop = 1;
2295
2296 if (btv->width < c.min_scaled_width)
2297 btv->width = c.min_scaled_width;
2298 else if (btv->width > c.max_scaled_width)
2299 btv->width = c.max_scaled_width;
2300
2301 if (btv->height < c.min_scaled_height)
2302 btv->height = c.min_scaled_height;
2303 else if (btv->height > c.max_scaled_height)
2304 btv->height = c.max_scaled_height;
2305
2306 return 0;
2307 }
2308
2309 static const struct v4l2_file_operations bttv_fops =
2310 {
2311 .owner = THIS_MODULE,
2312 .open = v4l2_fh_open,
2313 .release = vb2_fop_release,
2314 .unlocked_ioctl = video_ioctl2,
2315 .read = vb2_fop_read,
2316 .mmap = vb2_fop_mmap,
2317 .poll = vb2_fop_poll,
2318 };
2319
2320 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
2321 .vidioc_querycap = bttv_querycap,
2322 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
2323 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
2324 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
2325 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
2326 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
2327 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
2328 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
2329 .vidioc_g_pixelaspect = bttv_g_pixelaspect,
2330 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2331 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2332 .vidioc_querybuf = vb2_ioctl_querybuf,
2333 .vidioc_qbuf = vb2_ioctl_qbuf,
2334 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2335 .vidioc_streamon = vb2_ioctl_streamon,
2336 .vidioc_streamoff = vb2_ioctl_streamoff,
2337 .vidioc_s_std = bttv_s_std,
2338 .vidioc_g_std = bttv_g_std,
2339 .vidioc_enum_input = bttv_enum_input,
2340 .vidioc_g_input = bttv_g_input,
2341 .vidioc_s_input = bttv_s_input,
2342 .vidioc_g_tuner = bttv_g_tuner,
2343 .vidioc_s_tuner = bttv_s_tuner,
2344 .vidioc_g_selection = bttv_g_selection,
2345 .vidioc_s_selection = bttv_s_selection,
2346 .vidioc_g_parm = bttv_g_parm,
2347 .vidioc_g_frequency = bttv_g_frequency,
2348 .vidioc_s_frequency = bttv_s_frequency,
2349 .vidioc_log_status = bttv_log_status,
2350 .vidioc_querystd = bttv_querystd,
2351 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2352 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2353 #ifdef CONFIG_VIDEO_ADV_DEBUG
2354 .vidioc_g_register = bttv_g_register,
2355 .vidioc_s_register = bttv_s_register,
2356 #endif
2357 };
2358
2359 static struct video_device bttv_video_template = {
2360 .fops = &bttv_fops,
2361 .ioctl_ops = &bttv_ioctl_ops,
2362 .tvnorms = BTTV_NORMS,
2363 };
2364
2365 /* ----------------------------------------------------------------------- */
2366 /* radio interface */
2367
radio_open(struct file * file)2368 static int radio_open(struct file *file)
2369 {
2370 struct video_device *vdev = video_devdata(file);
2371 struct bttv *btv = video_drvdata(file);
2372 int ret = v4l2_fh_open(file);
2373
2374 if (ret)
2375 return ret;
2376
2377 dprintk("open dev=%s\n", video_device_node_name(vdev));
2378 dprintk("%d: open called (radio)\n", btv->c.nr);
2379
2380 btv->radio_user++;
2381 audio_mute(btv, btv->mute);
2382
2383 return 0;
2384 }
2385
radio_release(struct file * file)2386 static int radio_release(struct file *file)
2387 {
2388 struct bttv *btv = video_drvdata(file);
2389 struct saa6588_command cmd;
2390
2391 btv->radio_user--;
2392
2393 bttv_call_all(btv, core, command, SAA6588_CMD_CLOSE, &cmd);
2394
2395 if (btv->radio_user == 0)
2396 btv->has_radio_tuner = 0;
2397
2398 v4l2_fh_release(file);
2399
2400 return 0;
2401 }
2402
radio_g_tuner(struct file * file,void * priv,struct v4l2_tuner * t)2403 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2404 {
2405 struct bttv *btv = video_drvdata(file);
2406
2407 if (0 != t->index)
2408 return -EINVAL;
2409 strscpy(t->name, "Radio", sizeof(t->name));
2410 t->type = V4L2_TUNER_RADIO;
2411 radio_enable(btv);
2412
2413 bttv_call_all(btv, tuner, g_tuner, t);
2414
2415 if (btv->audio_mode_gpio)
2416 btv->audio_mode_gpio(btv, t, 0);
2417
2418 if (btv->has_tea575x)
2419 return snd_tea575x_g_tuner(&btv->tea, t);
2420
2421 return 0;
2422 }
2423
radio_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * t)2424 static int radio_s_tuner(struct file *file, void *priv,
2425 const struct v4l2_tuner *t)
2426 {
2427 struct bttv *btv = video_drvdata(file);
2428
2429 if (0 != t->index)
2430 return -EINVAL;
2431
2432 radio_enable(btv);
2433 bttv_call_all(btv, tuner, s_tuner, t);
2434 return 0;
2435 }
2436
radio_s_hw_freq_seek(struct file * file,void * priv,const struct v4l2_hw_freq_seek * a)2437 static int radio_s_hw_freq_seek(struct file *file, void *priv,
2438 const struct v4l2_hw_freq_seek *a)
2439 {
2440 struct bttv *btv = video_drvdata(file);
2441
2442 if (btv->has_tea575x)
2443 return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
2444
2445 return -ENOTTY;
2446 }
2447
radio_enum_freq_bands(struct file * file,void * priv,struct v4l2_frequency_band * band)2448 static int radio_enum_freq_bands(struct file *file, void *priv,
2449 struct v4l2_frequency_band *band)
2450 {
2451 struct bttv *btv = video_drvdata(file);
2452
2453 if (btv->has_tea575x)
2454 return snd_tea575x_enum_freq_bands(&btv->tea, band);
2455
2456 return -ENOTTY;
2457 }
2458
radio_read(struct file * file,char __user * data,size_t count,loff_t * ppos)2459 static ssize_t radio_read(struct file *file, char __user *data,
2460 size_t count, loff_t *ppos)
2461 {
2462 struct bttv *btv = video_drvdata(file);
2463 struct saa6588_command cmd;
2464
2465 cmd.block_count = count / 3;
2466 cmd.nonblocking = file->f_flags & O_NONBLOCK;
2467 cmd.buffer = data;
2468 cmd.instance = file;
2469 cmd.result = -ENODEV;
2470 radio_enable(btv);
2471
2472 bttv_call_all(btv, core, command, SAA6588_CMD_READ, &cmd);
2473
2474 return cmd.result;
2475 }
2476
radio_poll(struct file * file,poll_table * wait)2477 static __poll_t radio_poll(struct file *file, poll_table *wait)
2478 {
2479 struct bttv *btv = video_drvdata(file);
2480 struct saa6588_command cmd;
2481 __poll_t rc = v4l2_ctrl_poll(file, wait);
2482
2483 radio_enable(btv);
2484 cmd.instance = file;
2485 cmd.event_list = wait;
2486 cmd.poll_mask = 0;
2487 bttv_call_all(btv, core, command, SAA6588_CMD_POLL, &cmd);
2488
2489 return rc | cmd.poll_mask;
2490 }
2491
2492 static const struct v4l2_file_operations radio_fops =
2493 {
2494 .owner = THIS_MODULE,
2495 .open = radio_open,
2496 .read = radio_read,
2497 .release = radio_release,
2498 .unlocked_ioctl = video_ioctl2,
2499 .poll = radio_poll,
2500 };
2501
2502 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2503 .vidioc_querycap = bttv_querycap,
2504 .vidioc_log_status = bttv_log_status,
2505 .vidioc_g_tuner = radio_g_tuner,
2506 .vidioc_s_tuner = radio_s_tuner,
2507 .vidioc_g_frequency = bttv_g_frequency,
2508 .vidioc_s_frequency = bttv_s_frequency,
2509 .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
2510 .vidioc_enum_freq_bands = radio_enum_freq_bands,
2511 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2512 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2513 };
2514
2515 static struct video_device radio_template = {
2516 .fops = &radio_fops,
2517 .ioctl_ops = &radio_ioctl_ops,
2518 };
2519
2520 /* ----------------------------------------------------------------------- */
2521 /* some debug code */
2522
bttv_risc_decode(u32 risc)2523 static int bttv_risc_decode(u32 risc)
2524 {
2525 static char *instr[16] = {
2526 [ BT848_RISC_WRITE >> 28 ] = "write",
2527 [ BT848_RISC_SKIP >> 28 ] = "skip",
2528 [ BT848_RISC_WRITEC >> 28 ] = "writec",
2529 [ BT848_RISC_JUMP >> 28 ] = "jump",
2530 [ BT848_RISC_SYNC >> 28 ] = "sync",
2531 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
2532 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
2533 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
2534 };
2535 static int incr[16] = {
2536 [ BT848_RISC_WRITE >> 28 ] = 2,
2537 [ BT848_RISC_JUMP >> 28 ] = 2,
2538 [ BT848_RISC_SYNC >> 28 ] = 2,
2539 [ BT848_RISC_WRITE123 >> 28 ] = 5,
2540 [ BT848_RISC_SKIP123 >> 28 ] = 2,
2541 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
2542 };
2543 static char *bits[] = {
2544 "be0", "be1", "be2", "be3/resync",
2545 "set0", "set1", "set2", "set3",
2546 "clr0", "clr1", "clr2", "clr3",
2547 "irq", "res", "eol", "sol",
2548 };
2549 int i;
2550
2551 pr_cont("0x%08x [ %s", risc,
2552 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
2553 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
2554 if (risc & (1 << (i + 12)))
2555 pr_cont(" %s", bits[i]);
2556 pr_cont(" count=%d ]\n", risc & 0xfff);
2557 return incr[risc >> 28] ? incr[risc >> 28] : 1;
2558 }
2559
bttv_risc_disasm(struct bttv * btv,struct btcx_riscmem * risc)2560 static void bttv_risc_disasm(struct bttv *btv,
2561 struct btcx_riscmem *risc)
2562 {
2563 unsigned int i,j,n;
2564
2565 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
2566 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
2567 for (i = 0; i < (risc->size >> 2); i += n) {
2568 pr_info("%s: 0x%lx: ",
2569 btv->c.v4l2_dev.name,
2570 (unsigned long)(risc->dma + (i<<2)));
2571 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
2572 for (j = 1; j < n; j++)
2573 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
2574 btv->c.v4l2_dev.name,
2575 (unsigned long)(risc->dma + ((i+j)<<2)),
2576 risc->cpu[i+j], j);
2577 if (0 == risc->cpu[i])
2578 break;
2579 }
2580 }
2581
bttv_print_riscaddr(struct bttv * btv)2582 static void bttv_print_riscaddr(struct bttv *btv)
2583 {
2584 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
2585 pr_info(" vbi : o=%08llx e=%08llx\n",
2586 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
2587 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
2588 pr_info(" cap : o=%08llx e=%08llx\n",
2589 btv->curr.top
2590 ? (unsigned long long)btv->curr.top->top.dma : 0,
2591 btv->curr.bottom
2592 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
2593 bttv_risc_disasm(btv, &btv->main);
2594 }
2595
2596 /* ----------------------------------------------------------------------- */
2597 /* irq handler */
2598
2599 static char *irq_name[] = {
2600 "FMTCHG", // format change detected (525 vs. 625)
2601 "VSYNC", // vertical sync (new field)
2602 "HSYNC", // horizontal sync
2603 "OFLOW", // chroma/luma AGC overflow
2604 "HLOCK", // horizontal lock changed
2605 "VPRES", // video presence changed
2606 "6", "7",
2607 "I2CDONE", // hw irc operation finished
2608 "GPINT", // gpio port triggered irq
2609 "10",
2610 "RISCI", // risc instruction triggered irq
2611 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
2612 "FTRGT", // pixel data fifo overrun
2613 "FDSR", // fifo data stream resyncronisation
2614 "PPERR", // parity error (data transfer)
2615 "RIPERR", // parity error (read risc instructions)
2616 "PABORT", // pci abort
2617 "OCERR", // risc instruction error
2618 "SCERR", // syncronisation error
2619 };
2620
bttv_print_irqbits(u32 print,u32 mark)2621 static void bttv_print_irqbits(u32 print, u32 mark)
2622 {
2623 unsigned int i;
2624
2625 pr_cont("bits:");
2626 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
2627 if (print & (1 << i))
2628 pr_cont(" %s", irq_name[i]);
2629 if (mark & (1 << i))
2630 pr_cont("*");
2631 }
2632 }
2633
bttv_irq_debug_low_latency(struct bttv * btv,u32 rc)2634 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
2635 {
2636 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
2637 btv->c.nr,
2638 (unsigned long)btv->main.dma,
2639 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
2640 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
2641 (unsigned long)rc);
2642
2643 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
2644 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
2645 btv->c.nr);
2646 return;
2647 }
2648 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
2649 btv->c.nr);
2650 pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
2651 btv->c.nr);
2652 dump_stack();
2653 }
2654
2655 static int
bttv_irq_next_video(struct bttv * btv,struct bttv_buffer_set * set)2656 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
2657 {
2658 struct bttv_buffer *item;
2659
2660 memset(set,0,sizeof(*set));
2661
2662 /* capture request ? */
2663 if (!list_empty(&btv->capture)) {
2664 set->frame_irq = BT848_RISC_VIDEO;
2665 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2666
2667 if (V4L2_FIELD_HAS_TOP(item->vbuf.field))
2668 set->top = item;
2669 if (V4L2_FIELD_HAS_BOTTOM(item->vbuf.field))
2670 set->bottom = item;
2671
2672 /* capture request for other field ? */
2673 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field) &&
2674 item->list.next != &btv->capture) {
2675 item = list_entry(item->list.next,
2676 struct bttv_buffer, list);
2677 /* Mike Isely <isely@pobox.com> - Only check
2678 * and set up the bottom field in the logic
2679 * below. Don't ever do the top field. This
2680 * of course means that if we set up the
2681 * bottom field in the above code that we'll
2682 * actually skip a field. But that's OK.
2683 * Having processed only a single buffer this
2684 * time, then the next time around the first
2685 * available buffer should be for a top field.
2686 * That will then cause us here to set up a
2687 * top then a bottom field in the normal way.
2688 * The alternative to this understanding is
2689 * that we set up the second available buffer
2690 * as a top field, but that's out of order
2691 * since this driver always processes the top
2692 * field first - the effect will be the two
2693 * buffers being returned in the wrong order,
2694 * with the second buffer also being delayed
2695 * by one field time (owing to the fifo nature
2696 * of videobuf). Worse still, we'll be stuck
2697 * doing fields out of order now every time
2698 * until something else causes a field to be
2699 * dropped. By effectively forcing a field to
2700 * drop this way then we always get back into
2701 * sync within a single frame time. (Out of
2702 * order fields can screw up deinterlacing
2703 * algorithms.) */
2704 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field)) {
2705 if (!set->bottom &&
2706 item->vbuf.field == V4L2_FIELD_BOTTOM)
2707 set->bottom = item;
2708 if (set->top && set->bottom) {
2709 /*
2710 * The buffer set has a top buffer and
2711 * a bottom buffer and they are not
2712 * copies of each other.
2713 */
2714 set->top_irq = BT848_RISC_TOP;
2715 }
2716 }
2717 }
2718 }
2719
2720 dprintk("%d: next set: top=%p bottom=%p [irq=%d,%d]\n",
2721 btv->c.nr, set->top, set->bottom,
2722 set->frame_irq, set->top_irq);
2723 return 0;
2724 }
2725
2726 static void
bttv_irq_wakeup_video(struct bttv * btv,struct bttv_buffer_set * wakeup,struct bttv_buffer_set * curr,unsigned int state)2727 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
2728 struct bttv_buffer_set *curr, unsigned int state)
2729 {
2730 u64 ts = ktime_get_ns();
2731
2732 if (wakeup->top == wakeup->bottom) {
2733 if (NULL != wakeup->top && curr->top != wakeup->top) {
2734 if (irq_debug > 1)
2735 pr_debug("%d: wakeup: both=%p\n",
2736 btv->c.nr, wakeup->top);
2737 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2738 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2739 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2740 if (btv->field_count == 0)
2741 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2742 }
2743 } else {
2744 if (NULL != wakeup->top && curr->top != wakeup->top) {
2745 if (irq_debug > 1)
2746 pr_debug("%d: wakeup: top=%p\n",
2747 btv->c.nr, wakeup->top);
2748 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2749 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2750 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2751 if (btv->field_count == 0)
2752 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2753 }
2754 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
2755 if (irq_debug > 1)
2756 pr_debug("%d: wakeup: bottom=%p\n",
2757 btv->c.nr, wakeup->bottom);
2758 wakeup->bottom->vbuf.vb2_buf.timestamp = ts;
2759 wakeup->bottom->vbuf.sequence = btv->field_count >> 1;
2760 vb2_buffer_done(&wakeup->bottom->vbuf.vb2_buf, state);
2761 if (btv->field_count == 0)
2762 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2763 }
2764 }
2765 }
2766
2767 static void
bttv_irq_wakeup_vbi(struct bttv * btv,struct bttv_buffer * wakeup,unsigned int state)2768 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
2769 unsigned int state)
2770 {
2771 if (NULL == wakeup)
2772 return;
2773 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2774 wakeup->vbuf.sequence = btv->field_count >> 1;
2775
2776 /*
2777 * Ugly hack for backwards compatibility.
2778 * Some applications expect that the last 4 bytes of
2779 * the VBI data contains the sequence number.
2780 *
2781 * This makes it possible to associate the VBI data
2782 * with the video frame if you use read() to get the
2783 * VBI data.
2784 */
2785 if (vb2_fileio_is_active(wakeup->vbuf.vb2_buf.vb2_queue)) {
2786 u32 *vaddr = vb2_plane_vaddr(&wakeup->vbuf.vb2_buf, 0);
2787 unsigned long size =
2788 vb2_get_plane_payload(&wakeup->vbuf.vb2_buf, 0) / 4;
2789
2790 if (vaddr && size) {
2791 vaddr += size - 1;
2792 *vaddr = wakeup->vbuf.sequence;
2793 }
2794 }
2795
2796 vb2_buffer_done(&wakeup->vbuf.vb2_buf, state);
2797 if (btv->field_count == 0)
2798 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2799 }
2800
bttv_irq_timeout(struct timer_list * t)2801 static void bttv_irq_timeout(struct timer_list *t)
2802 {
2803 struct bttv *btv = from_timer(btv, t, timeout);
2804 struct bttv_buffer_set old,new;
2805 struct bttv_buffer *ovbi;
2806 struct bttv_buffer *item;
2807 unsigned long flags;
2808 int seqnr = 0;
2809
2810 if (bttv_verbose) {
2811 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
2812 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
2813 btread(BT848_RISC_COUNT));
2814 bttv_print_irqbits(btread(BT848_INT_STAT),0);
2815 pr_cont("\n");
2816 }
2817
2818 spin_lock_irqsave(&btv->s_lock,flags);
2819
2820 /* deactivate stuff */
2821 memset(&new,0,sizeof(new));
2822 old = btv->curr;
2823 ovbi = btv->cvbi;
2824 btv->curr = new;
2825 btv->cvbi = NULL;
2826 btv->loop_irq = 0;
2827 bttv_buffer_activate_video(btv, &new);
2828 bttv_buffer_activate_vbi(btv, NULL);
2829 bttv_set_dma(btv, 0);
2830
2831 /* wake up */
2832 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2833 bttv_irq_wakeup_vbi(btv, ovbi, VB2_BUF_STATE_DONE);
2834
2835 /* cancel all outstanding capture / vbi requests */
2836 if (btv->field_count)
2837 seqnr++;
2838 while (!list_empty(&btv->capture)) {
2839 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2840 list_del(&item->list);
2841 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2842 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2843 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2844 }
2845 while (!list_empty(&btv->vcapture)) {
2846 item = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2847 list_del(&item->list);
2848 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2849 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2850 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2851 }
2852
2853 btv->errors++;
2854 spin_unlock_irqrestore(&btv->s_lock,flags);
2855 }
2856
2857 static void
bttv_irq_wakeup_top(struct bttv * btv)2858 bttv_irq_wakeup_top(struct bttv *btv)
2859 {
2860 struct bttv_buffer *wakeup = btv->curr.top;
2861
2862 if (NULL == wakeup)
2863 return;
2864
2865 spin_lock(&btv->s_lock);
2866 btv->curr.top_irq = 0;
2867 btv->curr.top = NULL;
2868 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
2869 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2870 wakeup->vbuf.sequence = btv->field_count >> 1;
2871 vb2_buffer_done(&wakeup->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
2872 if (btv->field_count == 0)
2873 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2874 spin_unlock(&btv->s_lock);
2875 }
2876
is_active(struct btcx_riscmem * risc,u32 rc)2877 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
2878 {
2879 if (rc < risc->dma)
2880 return 0;
2881 if (rc > risc->dma + risc->size)
2882 return 0;
2883 return 1;
2884 }
2885
2886 static void
bttv_irq_switch_video(struct bttv * btv)2887 bttv_irq_switch_video(struct bttv *btv)
2888 {
2889 struct bttv_buffer_set new;
2890 struct bttv_buffer_set old;
2891 dma_addr_t rc;
2892
2893 spin_lock(&btv->s_lock);
2894
2895 /* new buffer set */
2896 bttv_irq_next_video(btv, &new);
2897 rc = btread(BT848_RISC_COUNT);
2898 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
2899 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
2900 btv->framedrop++;
2901 if (debug_latency)
2902 bttv_irq_debug_low_latency(btv, rc);
2903 spin_unlock(&btv->s_lock);
2904 return;
2905 }
2906
2907 /* switch over */
2908 old = btv->curr;
2909 btv->curr = new;
2910 btv->loop_irq &= ~BT848_RISC_VIDEO;
2911 bttv_buffer_activate_video(btv, &new);
2912 bttv_set_dma(btv, 0);
2913
2914 /* switch input */
2915 if (UNSET != btv->new_input) {
2916 video_mux(btv,btv->new_input);
2917 btv->new_input = UNSET;
2918 }
2919
2920 /* wake up finished buffers */
2921 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2922 spin_unlock(&btv->s_lock);
2923 }
2924
2925 static void
bttv_irq_switch_vbi(struct bttv * btv)2926 bttv_irq_switch_vbi(struct bttv *btv)
2927 {
2928 struct bttv_buffer *new = NULL;
2929 struct bttv_buffer *old;
2930 u32 rc;
2931
2932 spin_lock(&btv->s_lock);
2933
2934 if (!list_empty(&btv->vcapture))
2935 new = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2936 old = btv->cvbi;
2937
2938 rc = btread(BT848_RISC_COUNT);
2939 if (NULL != old && (is_active(&old->top, rc) ||
2940 is_active(&old->bottom, rc))) {
2941 btv->framedrop++;
2942 if (debug_latency)
2943 bttv_irq_debug_low_latency(btv, rc);
2944 spin_unlock(&btv->s_lock);
2945 return;
2946 }
2947
2948 /* switch */
2949 btv->cvbi = new;
2950 btv->loop_irq &= ~BT848_RISC_VBI;
2951 bttv_buffer_activate_vbi(btv, new);
2952 bttv_set_dma(btv, 0);
2953
2954 bttv_irq_wakeup_vbi(btv, old, VB2_BUF_STATE_DONE);
2955 spin_unlock(&btv->s_lock);
2956 }
2957
bttv_irq(int irq,void * dev_id)2958 static irqreturn_t bttv_irq(int irq, void *dev_id)
2959 {
2960 u32 stat,astat;
2961 u32 dstat;
2962 int count;
2963 struct bttv *btv;
2964 int handled = 0;
2965
2966 btv=(struct bttv *)dev_id;
2967
2968 count=0;
2969 while (1) {
2970 /* get/clear interrupt status bits */
2971 stat=btread(BT848_INT_STAT);
2972 astat=stat&btread(BT848_INT_MASK);
2973 if (!astat)
2974 break;
2975 handled = 1;
2976 btwrite(stat,BT848_INT_STAT);
2977
2978 /* get device status bits */
2979 dstat=btread(BT848_DSTATUS);
2980
2981 if (irq_debug) {
2982 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
2983 btv->c.nr, count, btv->field_count,
2984 stat>>28, btread(BT848_RISC_COUNT));
2985 bttv_print_irqbits(stat,astat);
2986 if (stat & BT848_INT_HLOCK)
2987 pr_cont(" HLOC => %s",
2988 dstat & BT848_DSTATUS_HLOC
2989 ? "yes" : "no");
2990 if (stat & BT848_INT_VPRES)
2991 pr_cont(" PRES => %s",
2992 dstat & BT848_DSTATUS_PRES
2993 ? "yes" : "no");
2994 if (stat & BT848_INT_FMTCHG)
2995 pr_cont(" NUML => %s",
2996 dstat & BT848_DSTATUS_NUML
2997 ? "625" : "525");
2998 pr_cont("\n");
2999 }
3000
3001 if (astat&BT848_INT_VSYNC)
3002 btv->field_count++;
3003
3004 if ((astat & BT848_INT_GPINT) && btv->remote) {
3005 bttv_input_irq(btv);
3006 }
3007
3008 if (astat & BT848_INT_I2CDONE) {
3009 btv->i2c_done = stat;
3010 wake_up(&btv->i2c_queue);
3011 }
3012
3013 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VBI))
3014 bttv_irq_switch_vbi(btv);
3015
3016 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_TOP))
3017 bttv_irq_wakeup_top(btv);
3018
3019 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VIDEO))
3020 bttv_irq_switch_video(btv);
3021
3022 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3023 /* trigger automute */
3024 audio_mux_gpio(btv, btv->audio_input, btv->mute);
3025
3026 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3027 pr_info("%d: %s%s @ %08x,",
3028 btv->c.nr,
3029 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3030 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3031 btread(BT848_RISC_COUNT));
3032 bttv_print_irqbits(stat,astat);
3033 pr_cont("\n");
3034 if (bttv_debug)
3035 bttv_print_riscaddr(btv);
3036 }
3037 if (fdsr && astat & BT848_INT_FDSR) {
3038 pr_info("%d: FDSR @ %08x\n",
3039 btv->c.nr, btread(BT848_RISC_COUNT));
3040 if (bttv_debug)
3041 bttv_print_riscaddr(btv);
3042 }
3043
3044 count++;
3045 if (count > 4) {
3046
3047 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3048 btwrite(0, BT848_INT_MASK);
3049
3050 pr_err("%d: IRQ lockup, cleared int mask [",
3051 btv->c.nr);
3052 } else {
3053 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3054 btv->c.nr);
3055
3056 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3057 BT848_INT_MASK);
3058 }
3059
3060 bttv_print_irqbits(stat,astat);
3061
3062 pr_cont("]\n");
3063 }
3064 }
3065 btv->irq_total++;
3066 if (handled)
3067 btv->irq_me++;
3068 return IRQ_RETVAL(handled);
3069 }
3070
3071
3072 /* ----------------------------------------------------------------------- */
3073 /* initialization */
3074
vdev_init(struct bttv * btv,struct video_device * vfd,const struct video_device * template,const char * type_name)3075 static int vdev_init(struct bttv *btv, struct video_device *vfd,
3076 const struct video_device *template,
3077 const char *type_name)
3078 {
3079 int err;
3080 struct vb2_queue *q;
3081 *vfd = *template;
3082 vfd->v4l2_dev = &btv->c.v4l2_dev;
3083 vfd->release = video_device_release_empty;
3084 video_set_drvdata(vfd, btv);
3085 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3086 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3087 type_name, bttv_tvcards[btv->c.type].name);
3088 if (btv->tuner_type == TUNER_ABSENT) {
3089 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3090 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3091 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3092 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3093 }
3094
3095 if (strcmp(type_name, "radio") == 0)
3096 return 0;
3097
3098 if (strcmp(type_name, "video") == 0) {
3099 q = &btv->capq;
3100 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3101 q->ops = &bttv_video_qops;
3102 } else if (strcmp(type_name, "vbi") == 0) {
3103 q = &btv->vbiq;
3104 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
3105 q->ops = &bttv_vbi_qops;
3106 } else {
3107 return -EINVAL;
3108 }
3109 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
3110 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
3111 q->mem_ops = &vb2_dma_sg_memops;
3112 q->drv_priv = btv;
3113 q->gfp_flags = __GFP_DMA32;
3114 q->buf_struct_size = sizeof(struct bttv_buffer);
3115 q->lock = &btv->lock;
3116 q->min_queued_buffers = 2;
3117 q->dev = &btv->c.pci->dev;
3118 err = vb2_queue_init(q);
3119 if (err)
3120 return err;
3121 vfd->queue = q;
3122
3123 return 0;
3124 }
3125
bttv_unregister_video(struct bttv * btv)3126 static void bttv_unregister_video(struct bttv *btv)
3127 {
3128 video_unregister_device(&btv->video_dev);
3129 video_unregister_device(&btv->vbi_dev);
3130 video_unregister_device(&btv->radio_dev);
3131 }
3132
3133 /* register video4linux devices */
bttv_register_video(struct bttv * btv)3134 static int bttv_register_video(struct bttv *btv)
3135 {
3136 /* video */
3137 vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3138 btv->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
3139 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
3140 if (btv->tuner_type != TUNER_ABSENT)
3141 btv->video_dev.device_caps |= V4L2_CAP_TUNER;
3142
3143 if (video_register_device(&btv->video_dev, VFL_TYPE_VIDEO,
3144 video_nr[btv->c.nr]) < 0)
3145 goto err;
3146 pr_info("%d: registered device %s\n",
3147 btv->c.nr, video_device_node_name(&btv->video_dev));
3148 if (device_create_file(&btv->video_dev.dev,
3149 &dev_attr_card)<0) {
3150 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3151 goto err;
3152 }
3153
3154 /* vbi */
3155 vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3156 btv->vbi_dev.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
3157 V4L2_CAP_STREAMING;
3158 if (btv->tuner_type != TUNER_ABSENT)
3159 btv->vbi_dev.device_caps |= V4L2_CAP_TUNER;
3160
3161 if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3162 vbi_nr[btv->c.nr]) < 0)
3163 goto err;
3164 pr_info("%d: registered device %s\n",
3165 btv->c.nr, video_device_node_name(&btv->vbi_dev));
3166
3167 if (!btv->has_radio)
3168 return 0;
3169 /* radio */
3170 vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
3171 btv->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
3172 if (btv->has_saa6588)
3173 btv->radio_dev.device_caps |= V4L2_CAP_READWRITE |
3174 V4L2_CAP_RDS_CAPTURE;
3175 if (btv->has_tea575x)
3176 btv->radio_dev.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
3177 btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
3178 if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3179 radio_nr[btv->c.nr]) < 0)
3180 goto err;
3181 pr_info("%d: registered device %s\n",
3182 btv->c.nr, video_device_node_name(&btv->radio_dev));
3183
3184 /* all done */
3185 return 0;
3186
3187 err:
3188 bttv_unregister_video(btv);
3189 return -1;
3190 }
3191
3192
3193 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3194 /* response on cards with no firmware is not enabled by OF */
pci_set_command(struct pci_dev * dev)3195 static void pci_set_command(struct pci_dev *dev)
3196 {
3197 #if defined(__powerpc__)
3198 unsigned int cmd;
3199
3200 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3201 cmd = (cmd | PCI_COMMAND_MEMORY );
3202 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3203 #endif
3204 }
3205
bttv_probe(struct pci_dev * dev,const struct pci_device_id * pci_id)3206 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3207 {
3208 struct v4l2_frequency init_freq = {
3209 .tuner = 0,
3210 .type = V4L2_TUNER_ANALOG_TV,
3211 .frequency = 980,
3212 };
3213 int result;
3214 unsigned char lat;
3215 struct bttv *btv;
3216 struct v4l2_ctrl_handler *hdl;
3217
3218 if (bttv_num == BTTV_MAX)
3219 return -ENOMEM;
3220 pr_info("Bt8xx card found (%d)\n", bttv_num);
3221 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
3222 if (btv == NULL) {
3223 pr_err("out of memory\n");
3224 return -ENOMEM;
3225 }
3226 btv->c.nr = bttv_num;
3227 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
3228 "bttv%d", btv->c.nr);
3229
3230 /* initialize structs / fill in defaults */
3231 mutex_init(&btv->lock);
3232 spin_lock_init(&btv->s_lock);
3233 spin_lock_init(&btv->gpio_lock);
3234 init_waitqueue_head(&btv->i2c_queue);
3235 INIT_LIST_HEAD(&btv->c.subs);
3236 INIT_LIST_HEAD(&btv->capture);
3237 INIT_LIST_HEAD(&btv->vcapture);
3238
3239 timer_setup(&btv->timeout, bttv_irq_timeout, 0);
3240
3241 btv->i2c_rc = -1;
3242 btv->tuner_type = UNSET;
3243 btv->new_input = UNSET;
3244 btv->has_radio=radio[btv->c.nr];
3245
3246 /* pci stuff (init, get irq/mmio, ... */
3247 btv->c.pci = dev;
3248 btv->id = dev->device;
3249 if (pci_enable_device(dev)) {
3250 pr_warn("%d: Can't enable device\n", btv->c.nr);
3251 result = -EIO;
3252 goto free_mem;
3253 }
3254 if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
3255 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
3256 result = -EIO;
3257 goto free_mem;
3258 }
3259 if (!request_mem_region(pci_resource_start(dev,0),
3260 pci_resource_len(dev,0),
3261 btv->c.v4l2_dev.name)) {
3262 pr_warn("%d: can't request iomem (0x%llx)\n",
3263 btv->c.nr,
3264 (unsigned long long)pci_resource_start(dev, 0));
3265 result = -EBUSY;
3266 goto free_mem;
3267 }
3268 pci_set_master(dev);
3269 pci_set_command(dev);
3270
3271 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
3272 if (result < 0) {
3273 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
3274 goto fail0;
3275 }
3276 hdl = &btv->ctrl_handler;
3277 v4l2_ctrl_handler_init(hdl, 20);
3278 btv->c.v4l2_dev.ctrl_handler = hdl;
3279 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
3280
3281 btv->revision = dev->revision;
3282 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3283 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
3284 bttv_num, btv->id, btv->revision, pci_name(dev),
3285 btv->c.pci->irq, lat,
3286 (unsigned long long)pci_resource_start(dev, 0));
3287 schedule();
3288
3289 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
3290 if (NULL == btv->bt848_mmio) {
3291 pr_err("%d: ioremap() failed\n", btv->c.nr);
3292 result = -EIO;
3293 goto fail1;
3294 }
3295
3296 /* identify card */
3297 bttv_idcard(btv);
3298
3299 /* disable irqs, register irq handler */
3300 btwrite(0, BT848_INT_MASK);
3301 result = request_irq(btv->c.pci->irq, bttv_irq,
3302 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
3303 if (result < 0) {
3304 pr_err("%d: can't get IRQ %d\n",
3305 bttv_num, btv->c.pci->irq);
3306 goto fail1;
3307 }
3308
3309 if (0 != bttv_handle_chipset(btv)) {
3310 result = -EIO;
3311 goto fail2;
3312 }
3313
3314 /* init options from insmod args */
3315 btv->opt_combfilter = combfilter;
3316 bttv_ctrl_combfilter.def = combfilter;
3317 bttv_ctrl_lumafilter.def = lumafilter;
3318 btv->opt_automute = automute;
3319 bttv_ctrl_automute.def = automute;
3320 bttv_ctrl_agc_crush.def = agc_crush;
3321 btv->opt_vcr_hack = vcr_hack;
3322 bttv_ctrl_vcr_hack.def = vcr_hack;
3323 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
3324 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
3325 btv->opt_uv_ratio = uv_ratio;
3326 bttv_ctrl_uv_ratio.def = uv_ratio;
3327 bttv_ctrl_full_luma.def = full_luma_range;
3328 bttv_ctrl_coring.def = coring;
3329
3330 /* fill struct bttv with some useful defaults */
3331 btv->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
3332 btv->width = 320;
3333 btv->height = 240;
3334 btv->field = V4L2_FIELD_INTERLACED;
3335 btv->input = 0;
3336 btv->tvnorm = 0; /* Index into bttv_tvnorms[] i.e. PAL. */
3337 bttv_vbi_fmt_reset(&btv->vbi_fmt, btv->tvnorm);
3338 btv->vbi_count[0] = VBI_DEFLINES;
3339 btv->vbi_count[1] = VBI_DEFLINES;
3340 btv->do_crop = 0;
3341
3342 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3343 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
3344 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3345 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
3346 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3347 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
3348 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3349 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
3350 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3351 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
3352 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3353 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
3354 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3355 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
3356 if (btv->volume_gpio)
3357 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3358 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
3359 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
3360 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
3361 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
3362 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
3363 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
3364 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
3365 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
3366 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
3367 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
3368 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
3369
3370 /* initialize hardware */
3371 if (bttv_gpio)
3372 bttv_gpio_tracking(btv,"pre-init");
3373
3374 bttv_risc_init_main(btv);
3375 init_bt848(btv);
3376
3377 /* gpio */
3378 btwrite(0x00, BT848_GPIO_REG_INP);
3379 btwrite(0x00, BT848_GPIO_OUT_EN);
3380 if (bttv_verbose)
3381 bttv_gpio_tracking(btv,"init");
3382
3383 /* needs to be done before i2c is registered */
3384 bttv_init_card1(btv);
3385
3386 /* register i2c + gpio */
3387 init_bttv_i2c(btv);
3388
3389 /* some card-specific stuff (needs working i2c) */
3390 bttv_init_card2(btv);
3391 bttv_init_tuner(btv);
3392 if (btv->tuner_type != TUNER_ABSENT) {
3393 bttv_set_frequency(btv, &init_freq);
3394 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
3395 }
3396 btv->std = V4L2_STD_PAL;
3397 init_irqreg(btv);
3398 if (!bttv_tvcards[btv->c.type].no_video)
3399 v4l2_ctrl_handler_setup(hdl);
3400 if (hdl->error) {
3401 result = hdl->error;
3402 goto fail2;
3403 }
3404 /* mute device */
3405 audio_mute(btv, 1);
3406
3407 /* register video4linux + input */
3408 if (!bttv_tvcards[btv->c.type].no_video) {
3409 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
3410 v4l2_ctrl_radio_filter, false);
3411 if (btv->radio_ctrl_handler.error) {
3412 result = btv->radio_ctrl_handler.error;
3413 goto fail2;
3414 }
3415 set_input(btv, btv->input, btv->tvnorm);
3416 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
3417 btv->crop[1] = btv->crop[0]; /* current = default */
3418 disclaim_vbi_lines(btv);
3419 disclaim_video_lines(btv);
3420 bttv_register_video(btv);
3421 }
3422
3423 /* add subdevices and autoload dvb-bt8xx if needed */
3424 if (bttv_tvcards[btv->c.type].has_dvb) {
3425 bttv_sub_add_device(&btv->c, "dvb");
3426 request_modules(btv);
3427 }
3428
3429 if (!disable_ir) {
3430 init_bttv_i2c_ir(btv);
3431 bttv_input_init(btv);
3432 }
3433
3434 /* everything is fine */
3435 bttv_num++;
3436 return 0;
3437
3438 fail2:
3439 free_irq(btv->c.pci->irq,btv);
3440
3441 fail1:
3442 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3443 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3444 v4l2_device_unregister(&btv->c.v4l2_dev);
3445
3446 fail0:
3447 if (btv->bt848_mmio)
3448 iounmap(btv->bt848_mmio);
3449 release_mem_region(pci_resource_start(btv->c.pci,0),
3450 pci_resource_len(btv->c.pci,0));
3451 pci_disable_device(btv->c.pci);
3452
3453 free_mem:
3454 bttvs[btv->c.nr] = NULL;
3455 kfree(btv);
3456 return result;
3457 }
3458
bttv_remove(struct pci_dev * pci_dev)3459 static void bttv_remove(struct pci_dev *pci_dev)
3460 {
3461 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
3462 struct bttv *btv = to_bttv(v4l2_dev);
3463
3464 if (bttv_verbose)
3465 pr_info("%d: unloading\n", btv->c.nr);
3466
3467 if (bttv_tvcards[btv->c.type].has_dvb)
3468 flush_request_modules(btv);
3469
3470 /* shutdown everything (DMA+IRQs) */
3471 btand(~15, BT848_GPIO_DMA_CTL);
3472 btwrite(0, BT848_INT_MASK);
3473 btwrite(~0x0, BT848_INT_STAT);
3474 btwrite(0x0, BT848_GPIO_OUT_EN);
3475 if (bttv_gpio)
3476 bttv_gpio_tracking(btv,"cleanup");
3477
3478 /* tell gpio modules we are leaving ... */
3479 btv->shutdown=1;
3480 bttv_input_fini(btv);
3481 bttv_sub_del_devices(&btv->c);
3482
3483 /* unregister i2c_bus + input */
3484 fini_bttv_i2c(btv);
3485
3486 /* unregister video4linux */
3487 bttv_unregister_video(btv);
3488
3489 /* free allocated memory */
3490 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3491 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3492 btcx_riscmem_free(btv->c.pci,&btv->main);
3493
3494 /* free resources */
3495 free_irq(btv->c.pci->irq,btv);
3496 del_timer_sync(&btv->timeout);
3497 iounmap(btv->bt848_mmio);
3498 release_mem_region(pci_resource_start(btv->c.pci,0),
3499 pci_resource_len(btv->c.pci,0));
3500 pci_disable_device(btv->c.pci);
3501
3502 v4l2_device_unregister(&btv->c.v4l2_dev);
3503 bttvs[btv->c.nr] = NULL;
3504 kfree(btv);
3505
3506 return;
3507 }
3508
bttv_suspend(struct device * dev)3509 static int __maybe_unused bttv_suspend(struct device *dev)
3510 {
3511 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3512 struct bttv *btv = to_bttv(v4l2_dev);
3513 struct bttv_buffer_set idle;
3514 unsigned long flags;
3515
3516 dprintk("%d: suspend\n", btv->c.nr);
3517
3518 /* stop dma + irqs */
3519 spin_lock_irqsave(&btv->s_lock,flags);
3520 memset(&idle, 0, sizeof(idle));
3521 btv->state.video = btv->curr;
3522 btv->state.vbi = btv->cvbi;
3523 btv->state.loop_irq = btv->loop_irq;
3524 btv->curr = idle;
3525 btv->loop_irq = 0;
3526 bttv_buffer_activate_video(btv, &idle);
3527 bttv_buffer_activate_vbi(btv, NULL);
3528 bttv_set_dma(btv, 0);
3529 btwrite(0, BT848_INT_MASK);
3530 spin_unlock_irqrestore(&btv->s_lock,flags);
3531
3532 /* save bt878 state */
3533 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3534 btv->state.gpio_data = gpio_read();
3535
3536 btv->state.disabled = 1;
3537 return 0;
3538 }
3539
bttv_resume(struct device * dev)3540 static int __maybe_unused bttv_resume(struct device *dev)
3541 {
3542 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3543 struct bttv *btv = to_bttv(v4l2_dev);
3544 unsigned long flags;
3545
3546 dprintk("%d: resume\n", btv->c.nr);
3547
3548 btv->state.disabled = 0;
3549
3550 /* restore bt878 state */
3551 bttv_reinit_bt848(btv);
3552 gpio_inout(0xffffff, btv->state.gpio_enable);
3553 gpio_write(btv->state.gpio_data);
3554
3555 /* restart dma */
3556 spin_lock_irqsave(&btv->s_lock,flags);
3557 btv->curr = btv->state.video;
3558 btv->cvbi = btv->state.vbi;
3559 btv->loop_irq = btv->state.loop_irq;
3560 bttv_buffer_activate_video(btv, &btv->curr);
3561 bttv_buffer_activate_vbi(btv, btv->cvbi);
3562 bttv_set_dma(btv, 0);
3563 spin_unlock_irqrestore(&btv->s_lock,flags);
3564 return 0;
3565 }
3566
3567 static const struct pci_device_id bttv_pci_tbl[] = {
3568 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
3569 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
3570 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
3571 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
3572 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
3573 {0,}
3574 };
3575
3576 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3577
3578 static SIMPLE_DEV_PM_OPS(bttv_pm_ops,
3579 bttv_suspend,
3580 bttv_resume);
3581
3582 static struct pci_driver bttv_pci_driver = {
3583 .name = "bttv",
3584 .id_table = bttv_pci_tbl,
3585 .probe = bttv_probe,
3586 .remove = bttv_remove,
3587 .driver.pm = &bttv_pm_ops,
3588 };
3589
bttv_init_module(void)3590 static int __init bttv_init_module(void)
3591 {
3592 int ret;
3593
3594 bttv_num = 0;
3595
3596 pr_info("driver version %s loaded\n", BTTV_VERSION);
3597 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
3598 gbuffers = 2;
3599 if (gbufsize > BTTV_MAX_FBUF)
3600 gbufsize = BTTV_MAX_FBUF;
3601 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
3602 if (bttv_verbose)
3603 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
3604 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
3605
3606 bttv_check_chipset();
3607
3608 ret = bus_register(&bttv_sub_bus_type);
3609 if (ret < 0) {
3610 pr_warn("bus_register error: %d\n", ret);
3611 return ret;
3612 }
3613 ret = pci_register_driver(&bttv_pci_driver);
3614 if (ret < 0)
3615 bus_unregister(&bttv_sub_bus_type);
3616
3617 return ret;
3618 }
3619
bttv_cleanup_module(void)3620 static void __exit bttv_cleanup_module(void)
3621 {
3622 pci_unregister_driver(&bttv_pci_driver);
3623 bus_unregister(&bttv_sub_bus_type);
3624 }
3625
3626 module_init(bttv_init_module);
3627 module_exit(bttv_cleanup_module);
3628