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