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