Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * |
3 | * device driver for philips saa7134 based TV cards | |
4 | * video4linux video interface | |
5 | * | |
6 | * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/init.h> | |
24 | #include <linux/list.h> | |
25 | #include <linux/module.h> | |
1da177e4 LT |
26 | #include <linux/kernel.h> |
27 | #include <linux/slab.h> | |
9040b32e | 28 | #include <linux/sort.h> |
1da177e4 | 29 | |
5e453dc7 | 30 | #include <media/v4l2-common.h> |
a2004502 | 31 | #include <media/v4l2-event.h> |
b9218f2f | 32 | #include <media/saa6588.h> |
1da177e4 | 33 | |
a2004502 HV |
34 | #include "saa7134-reg.h" |
35 | #include "saa7134.h" | |
36 | ||
1da177e4 LT |
37 | /* ------------------------------------------------------------------ */ |
38 | ||
8c85454a | 39 | unsigned int video_debug; |
1da177e4 | 40 | static unsigned int gbuffers = 8; |
ff699e6b | 41 | static unsigned int noninterlaced; /* 0 */ |
1da177e4 LT |
42 | static unsigned int gbufsize = 720*576*4; |
43 | static unsigned int gbufsize_max = 720*576*4; | |
17b10a73 | 44 | static char secam[] = "--"; |
1da177e4 LT |
45 | module_param(video_debug, int, 0644); |
46 | MODULE_PARM_DESC(video_debug,"enable debug messages [video]"); | |
47 | module_param(gbuffers, int, 0444); | |
48 | MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32"); | |
49 | module_param(noninterlaced, int, 0644); | |
d5fdd135 | 50 | MODULE_PARM_DESC(noninterlaced,"capture non interlaced video"); |
17b10a73 HH |
51 | module_param_string(secam, secam, sizeof(secam), 0644); |
52 | MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc"); | |
53 | ||
1da177e4 | 54 | |
8c85454a | 55 | #define dprintk(fmt, arg...) if (video_debug&0x04) \ |
1da177e4 LT |
56 | printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg) |
57 | ||
2f8d4f51 RB |
58 | /* ------------------------------------------------------------------ */ |
59 | /* Defines for Video Output Port Register at address 0x191 */ | |
60 | ||
61 | /* Bit 0: VIP code T bit polarity */ | |
62 | ||
63 | #define VP_T_CODE_P_NON_INVERTED 0x00 | |
64 | #define VP_T_CODE_P_INVERTED 0x01 | |
65 | ||
66 | /* ------------------------------------------------------------------ */ | |
67 | /* Defines for Video Output Port Register at address 0x195 */ | |
68 | ||
69 | /* Bit 2: Video output clock delay control */ | |
70 | ||
71 | #define VP_CLK_CTRL2_NOT_DELAYED 0x00 | |
72 | #define VP_CLK_CTRL2_DELAYED 0x04 | |
73 | ||
74 | /* Bit 1: Video output clock invert control */ | |
75 | ||
76 | #define VP_CLK_CTRL1_NON_INVERTED 0x00 | |
77 | #define VP_CLK_CTRL1_INVERTED 0x02 | |
78 | ||
79 | /* ------------------------------------------------------------------ */ | |
80 | /* Defines for Video Output Port Register at address 0x196 */ | |
81 | ||
82 | /* Bits 2 to 0: VSYNC pin video vertical sync type */ | |
83 | ||
84 | #define VP_VS_TYPE_MASK 0x07 | |
85 | ||
86 | #define VP_VS_TYPE_OFF 0x00 | |
87 | #define VP_VS_TYPE_V123 0x01 | |
88 | #define VP_VS_TYPE_V_ITU 0x02 | |
89 | #define VP_VS_TYPE_VGATE_L 0x03 | |
90 | #define VP_VS_TYPE_RESERVED1 0x04 | |
91 | #define VP_VS_TYPE_RESERVED2 0x05 | |
92 | #define VP_VS_TYPE_F_ITU 0x06 | |
93 | #define VP_VS_TYPE_SC_FID 0x07 | |
94 | ||
1da177e4 LT |
95 | /* ------------------------------------------------------------------ */ |
96 | /* data structs for video */ | |
97 | ||
98 | static int video_out[][9] = { | |
99 | [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 }, | |
100 | }; | |
101 | ||
102 | static struct saa7134_format formats[] = { | |
103 | { | |
104 | .name = "8 bpp gray", | |
105 | .fourcc = V4L2_PIX_FMT_GREY, | |
106 | .depth = 8, | |
107 | .pm = 0x06, | |
108 | },{ | |
109 | .name = "15 bpp RGB, le", | |
110 | .fourcc = V4L2_PIX_FMT_RGB555, | |
111 | .depth = 16, | |
112 | .pm = 0x13 | 0x80, | |
113 | },{ | |
114 | .name = "15 bpp RGB, be", | |
115 | .fourcc = V4L2_PIX_FMT_RGB555X, | |
116 | .depth = 16, | |
117 | .pm = 0x13 | 0x80, | |
118 | .bswap = 1, | |
119 | },{ | |
120 | .name = "16 bpp RGB, le", | |
121 | .fourcc = V4L2_PIX_FMT_RGB565, | |
122 | .depth = 16, | |
123 | .pm = 0x10 | 0x80, | |
124 | },{ | |
125 | .name = "16 bpp RGB, be", | |
126 | .fourcc = V4L2_PIX_FMT_RGB565X, | |
127 | .depth = 16, | |
128 | .pm = 0x10 | 0x80, | |
129 | .bswap = 1, | |
130 | },{ | |
131 | .name = "24 bpp RGB, le", | |
132 | .fourcc = V4L2_PIX_FMT_BGR24, | |
133 | .depth = 24, | |
134 | .pm = 0x11, | |
135 | },{ | |
136 | .name = "24 bpp RGB, be", | |
137 | .fourcc = V4L2_PIX_FMT_RGB24, | |
138 | .depth = 24, | |
139 | .pm = 0x11, | |
140 | .bswap = 1, | |
141 | },{ | |
142 | .name = "32 bpp RGB, le", | |
143 | .fourcc = V4L2_PIX_FMT_BGR32, | |
144 | .depth = 32, | |
145 | .pm = 0x12, | |
146 | },{ | |
147 | .name = "32 bpp RGB, be", | |
148 | .fourcc = V4L2_PIX_FMT_RGB32, | |
149 | .depth = 32, | |
150 | .pm = 0x12, | |
151 | .bswap = 1, | |
152 | .wswap = 1, | |
153 | },{ | |
154 | .name = "4:2:2 packed, YUYV", | |
155 | .fourcc = V4L2_PIX_FMT_YUYV, | |
156 | .depth = 16, | |
157 | .pm = 0x00, | |
158 | .bswap = 1, | |
159 | .yuv = 1, | |
160 | },{ | |
161 | .name = "4:2:2 packed, UYVY", | |
162 | .fourcc = V4L2_PIX_FMT_UYVY, | |
163 | .depth = 16, | |
164 | .pm = 0x00, | |
165 | .yuv = 1, | |
166 | },{ | |
167 | .name = "4:2:2 planar, Y-Cb-Cr", | |
168 | .fourcc = V4L2_PIX_FMT_YUV422P, | |
169 | .depth = 16, | |
170 | .pm = 0x09, | |
171 | .yuv = 1, | |
172 | .planar = 1, | |
173 | .hshift = 1, | |
174 | .vshift = 0, | |
175 | },{ | |
176 | .name = "4:2:0 planar, Y-Cb-Cr", | |
177 | .fourcc = V4L2_PIX_FMT_YUV420, | |
178 | .depth = 12, | |
179 | .pm = 0x0a, | |
180 | .yuv = 1, | |
181 | .planar = 1, | |
182 | .hshift = 1, | |
183 | .vshift = 1, | |
184 | },{ | |
185 | .name = "4:2:0 planar, Y-Cb-Cr", | |
186 | .fourcc = V4L2_PIX_FMT_YVU420, | |
187 | .depth = 12, | |
188 | .pm = 0x0a, | |
189 | .yuv = 1, | |
190 | .planar = 1, | |
191 | .uvswap = 1, | |
192 | .hshift = 1, | |
193 | .vshift = 1, | |
194 | } | |
195 | }; | |
196 | #define FORMATS ARRAY_SIZE(formats) | |
197 | ||
198 | #define NORM_625_50 \ | |
199 | .h_start = 0, \ | |
200 | .h_stop = 719, \ | |
201 | .video_v_start = 24, \ | |
202 | .video_v_stop = 311, \ | |
f246a817 MS |
203 | .vbi_v_start_0 = 7, \ |
204 | .vbi_v_stop_0 = 22, \ | |
205 | .vbi_v_start_1 = 319, \ | |
1da177e4 LT |
206 | .src_timing = 4 |
207 | ||
208 | #define NORM_525_60 \ | |
209 | .h_start = 0, \ | |
dd6ed861 | 210 | .h_stop = 719, \ |
f246a817 MS |
211 | .video_v_start = 23, \ |
212 | .video_v_stop = 262, \ | |
213 | .vbi_v_start_0 = 10, \ | |
214 | .vbi_v_stop_0 = 21, \ | |
215 | .vbi_v_start_1 = 273, \ | |
216 | .src_timing = 7 | |
1da177e4 LT |
217 | |
218 | static struct saa7134_tvnorm tvnorms[] = { | |
219 | { | |
220 | .name = "PAL", /* autodetect */ | |
221 | .id = V4L2_STD_PAL, | |
222 | NORM_625_50, | |
223 | ||
224 | .sync_control = 0x18, | |
225 | .luma_control = 0x40, | |
226 | .chroma_ctrl1 = 0x81, | |
227 | .chroma_gain = 0x2a, | |
228 | .chroma_ctrl2 = 0x06, | |
229 | .vgate_misc = 0x1c, | |
230 | ||
231 | },{ | |
232 | .name = "PAL-BG", | |
233 | .id = V4L2_STD_PAL_BG, | |
234 | NORM_625_50, | |
235 | ||
236 | .sync_control = 0x18, | |
237 | .luma_control = 0x40, | |
238 | .chroma_ctrl1 = 0x81, | |
239 | .chroma_gain = 0x2a, | |
240 | .chroma_ctrl2 = 0x06, | |
241 | .vgate_misc = 0x1c, | |
242 | ||
243 | },{ | |
244 | .name = "PAL-I", | |
245 | .id = V4L2_STD_PAL_I, | |
246 | NORM_625_50, | |
247 | ||
248 | .sync_control = 0x18, | |
249 | .luma_control = 0x40, | |
250 | .chroma_ctrl1 = 0x81, | |
251 | .chroma_gain = 0x2a, | |
252 | .chroma_ctrl2 = 0x06, | |
253 | .vgate_misc = 0x1c, | |
254 | ||
255 | },{ | |
256 | .name = "PAL-DK", | |
257 | .id = V4L2_STD_PAL_DK, | |
258 | NORM_625_50, | |
259 | ||
260 | .sync_control = 0x18, | |
261 | .luma_control = 0x40, | |
262 | .chroma_ctrl1 = 0x81, | |
263 | .chroma_gain = 0x2a, | |
264 | .chroma_ctrl2 = 0x06, | |
265 | .vgate_misc = 0x1c, | |
266 | ||
267 | },{ | |
268 | .name = "NTSC", | |
269 | .id = V4L2_STD_NTSC, | |
270 | NORM_525_60, | |
271 | ||
272 | .sync_control = 0x59, | |
273 | .luma_control = 0x40, | |
274 | .chroma_ctrl1 = 0x89, | |
275 | .chroma_gain = 0x2a, | |
276 | .chroma_ctrl2 = 0x0e, | |
277 | .vgate_misc = 0x18, | |
278 | ||
279 | },{ | |
280 | .name = "SECAM", | |
281 | .id = V4L2_STD_SECAM, | |
282 | NORM_625_50, | |
283 | ||
17b10a73 HH |
284 | .sync_control = 0x18, |
285 | .luma_control = 0x1b, | |
286 | .chroma_ctrl1 = 0xd1, | |
287 | .chroma_gain = 0x80, | |
288 | .chroma_ctrl2 = 0x00, | |
289 | .vgate_misc = 0x1c, | |
290 | ||
291 | },{ | |
292 | .name = "SECAM-DK", | |
293 | .id = V4L2_STD_SECAM_DK, | |
294 | NORM_625_50, | |
295 | ||
296 | .sync_control = 0x18, | |
297 | .luma_control = 0x1b, | |
298 | .chroma_ctrl1 = 0xd1, | |
299 | .chroma_gain = 0x80, | |
300 | .chroma_ctrl2 = 0x00, | |
301 | .vgate_misc = 0x1c, | |
302 | ||
303 | },{ | |
304 | .name = "SECAM-L", | |
305 | .id = V4L2_STD_SECAM_L, | |
306 | NORM_625_50, | |
307 | ||
308 | .sync_control = 0x18, | |
309 | .luma_control = 0x1b, | |
310 | .chroma_ctrl1 = 0xd1, | |
311 | .chroma_gain = 0x80, | |
312 | .chroma_ctrl2 = 0x00, | |
313 | .vgate_misc = 0x1c, | |
314 | ||
315 | },{ | |
316 | .name = "SECAM-Lc", | |
317 | .id = V4L2_STD_SECAM_LC, | |
318 | NORM_625_50, | |
319 | ||
320 | .sync_control = 0x18, | |
1da177e4 LT |
321 | .luma_control = 0x1b, |
322 | .chroma_ctrl1 = 0xd1, | |
323 | .chroma_gain = 0x80, | |
324 | .chroma_ctrl2 = 0x00, | |
325 | .vgate_misc = 0x1c, | |
326 | ||
327 | },{ | |
328 | .name = "PAL-M", | |
329 | .id = V4L2_STD_PAL_M, | |
330 | NORM_525_60, | |
331 | ||
332 | .sync_control = 0x59, | |
333 | .luma_control = 0x40, | |
334 | .chroma_ctrl1 = 0xb9, | |
335 | .chroma_gain = 0x2a, | |
336 | .chroma_ctrl2 = 0x0e, | |
337 | .vgate_misc = 0x18, | |
338 | ||
339 | },{ | |
340 | .name = "PAL-Nc", | |
341 | .id = V4L2_STD_PAL_Nc, | |
342 | NORM_625_50, | |
343 | ||
344 | .sync_control = 0x18, | |
345 | .luma_control = 0x40, | |
346 | .chroma_ctrl1 = 0xa1, | |
347 | .chroma_gain = 0x2a, | |
348 | .chroma_ctrl2 = 0x06, | |
349 | .vgate_misc = 0x1c, | |
350 | ||
351 | },{ | |
352 | .name = "PAL-60", | |
353 | .id = V4L2_STD_PAL_60, | |
354 | ||
355 | .h_start = 0, | |
356 | .h_stop = 719, | |
4ac97914 MCC |
357 | .video_v_start = 23, |
358 | .video_v_stop = 262, | |
359 | .vbi_v_start_0 = 10, | |
360 | .vbi_v_stop_0 = 21, | |
361 | .vbi_v_start_1 = 273, | |
362 | .src_timing = 7, | |
1da177e4 LT |
363 | |
364 | .sync_control = 0x18, | |
365 | .luma_control = 0x40, | |
366 | .chroma_ctrl1 = 0x81, | |
367 | .chroma_gain = 0x2a, | |
368 | .chroma_ctrl2 = 0x06, | |
369 | .vgate_misc = 0x1c, | |
370 | } | |
371 | }; | |
372 | #define TVNORMS ARRAY_SIZE(tvnorms) | |
373 | ||
1da177e4 LT |
374 | static struct saa7134_format* format_by_fourcc(unsigned int fourcc) |
375 | { | |
376 | unsigned int i; | |
377 | ||
378 | for (i = 0; i < FORMATS; i++) | |
379 | if (formats[i].fourcc == fourcc) | |
380 | return formats+i; | |
381 | return NULL; | |
382 | } | |
383 | ||
384 | /* ----------------------------------------------------------------------- */ | |
385 | /* resource management */ | |
386 | ||
387 | static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit) | |
388 | { | |
389 | if (fh->resources & bit) | |
390 | /* have it already allocated */ | |
391 | return 1; | |
392 | ||
393 | /* is it free? */ | |
3593cab5 | 394 | mutex_lock(&dev->lock); |
1da177e4 LT |
395 | if (dev->resources & bit) { |
396 | /* no, someone else uses it */ | |
3593cab5 | 397 | mutex_unlock(&dev->lock); |
1da177e4 LT |
398 | return 0; |
399 | } | |
400 | /* it's free, grab it */ | |
401 | fh->resources |= bit; | |
402 | dev->resources |= bit; | |
403 | dprintk("res: get %d\n",bit); | |
3593cab5 | 404 | mutex_unlock(&dev->lock); |
1da177e4 LT |
405 | return 1; |
406 | } | |
407 | ||
1da177e4 LT |
408 | static |
409 | void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits) | |
410 | { | |
ae24601b | 411 | BUG_ON((fh->resources & bits) != bits); |
1da177e4 | 412 | |
3593cab5 | 413 | mutex_lock(&dev->lock); |
1da177e4 LT |
414 | fh->resources &= ~bits; |
415 | dev->resources &= ~bits; | |
416 | dprintk("res: put %d\n",bits); | |
3593cab5 | 417 | mutex_unlock(&dev->lock); |
1da177e4 LT |
418 | } |
419 | ||
420 | /* ------------------------------------------------------------------ */ | |
421 | ||
b02044d8 | 422 | static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm) |
1da177e4 | 423 | { |
1da177e4 LT |
424 | dprintk("set tv norm = %s\n",norm->name); |
425 | dev->tvnorm = norm; | |
426 | ||
1da177e4 LT |
427 | /* setup cropping */ |
428 | dev->crop_bounds.left = norm->h_start; | |
429 | dev->crop_defrect.left = norm->h_start; | |
430 | dev->crop_bounds.width = norm->h_stop - norm->h_start +1; | |
431 | dev->crop_defrect.width = norm->h_stop - norm->h_start +1; | |
432 | ||
f246a817 | 433 | dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2; |
1da177e4 LT |
434 | dev->crop_defrect.top = norm->video_v_start*2; |
435 | dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624) | |
436 | - dev->crop_bounds.top; | |
437 | dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2; | |
438 | ||
439 | dev->crop_current = dev->crop_defrect; | |
440 | ||
c458473e | 441 | saa7134_set_tvnorm_hw(dev); |
cb71201f ML |
442 | } |
443 | ||
444 | static void video_mux(struct saa7134_dev *dev, int input) | |
445 | { | |
446 | dprintk("video input = %d [%s]\n", input, card_in(dev, input).name); | |
447 | dev->ctl_input = input; | |
448 | set_tvnorm(dev, dev->tvnorm); | |
449 | saa7134_tvaudio_setinput(dev, &card_in(dev, input)); | |
450 | } | |
451 | ||
c458473e ML |
452 | |
453 | static void saa7134_set_decoder(struct saa7134_dev *dev) | |
cb71201f ML |
454 | { |
455 | int luma_control, sync_control, mux; | |
456 | ||
457 | struct saa7134_tvnorm *norm = dev->tvnorm; | |
458 | mux = card_in(dev, dev->ctl_input).vmux; | |
459 | ||
460 | luma_control = norm->luma_control; | |
461 | sync_control = norm->sync_control; | |
462 | ||
463 | if (mux > 5) | |
464 | luma_control |= 0x80; /* svideo */ | |
465 | if (noninterlaced || dev->nosignal) | |
466 | sync_control |= 0x20; | |
467 | ||
1da177e4 LT |
468 | /* setup video decoder */ |
469 | saa_writeb(SAA7134_INCR_DELAY, 0x08); | |
470 | saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux); | |
471 | saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00); | |
472 | ||
473 | saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90); | |
474 | saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90); | |
475 | saa_writeb(SAA7134_HSYNC_START, 0xeb); | |
476 | saa_writeb(SAA7134_HSYNC_STOP, 0xe0); | |
477 | saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing); | |
478 | ||
479 | saa_writeb(SAA7134_SYNC_CTRL, sync_control); | |
480 | saa_writeb(SAA7134_LUMA_CTRL, luma_control); | |
481 | saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright); | |
1da177e4 | 482 | |
f5a1ac64 ML |
483 | saa_writeb(SAA7134_DEC_LUMA_CONTRAST, |
484 | dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast); | |
485 | ||
486 | saa_writeb(SAA7134_DEC_CHROMA_SATURATION, | |
487 | dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation); | |
488 | ||
1da177e4 LT |
489 | saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue); |
490 | saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1); | |
491 | saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain); | |
492 | ||
493 | saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2); | |
494 | saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00); | |
495 | ||
496 | saa_writeb(SAA7134_ANALOG_ADC, 0x01); | |
497 | saa_writeb(SAA7134_VGATE_START, 0x11); | |
498 | saa_writeb(SAA7134_VGATE_STOP, 0xfe); | |
499 | saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc); | |
500 | saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40); | |
501 | saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80); | |
1da177e4 LT |
502 | } |
503 | ||
c458473e ML |
504 | void saa7134_set_tvnorm_hw(struct saa7134_dev *dev) |
505 | { | |
506 | saa7134_set_decoder(dev); | |
507 | ||
7bff4b4d | 508 | if (card_in(dev, dev->ctl_input).tv) |
f41737ec | 509 | saa_call_all(dev, core, s_std, dev->tvnorm->id); |
9afb7377 HV |
510 | /* Set the correct norm for the saa6752hs. This function |
511 | does nothing if there is no saa6752hs. */ | |
f41737ec | 512 | saa_call_empress(dev, core, s_std, dev->tvnorm->id); |
c458473e ML |
513 | } |
514 | ||
1da177e4 LT |
515 | static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale) |
516 | { | |
517 | static const struct { | |
518 | int xpsc; | |
519 | int xacl; | |
520 | int xc2_1; | |
521 | int xdcg; | |
522 | int vpfy; | |
523 | } vals[] = { | |
524 | /* XPSC XACL XC2_1 XDCG VPFY */ | |
525 | { 1, 0, 0, 0, 0 }, | |
526 | { 2, 2, 1, 2, 2 }, | |
527 | { 3, 4, 1, 3, 2 }, | |
528 | { 4, 8, 1, 4, 2 }, | |
529 | { 5, 8, 1, 4, 2 }, | |
530 | { 6, 8, 1, 4, 3 }, | |
531 | { 7, 8, 1, 4, 3 }, | |
532 | { 8, 15, 0, 4, 3 }, | |
533 | { 9, 15, 0, 4, 3 }, | |
534 | { 10, 16, 1, 5, 3 }, | |
535 | }; | |
536 | static const int count = ARRAY_SIZE(vals); | |
537 | int i; | |
538 | ||
539 | for (i = 0; i < count; i++) | |
540 | if (vals[i].xpsc == prescale) | |
541 | break; | |
542 | if (i == count) | |
543 | return; | |
544 | ||
545 | saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc); | |
546 | saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl); | |
547 | saa_writeb(SAA7134_LEVEL_CTRL(task), | |
548 | (vals[i].xc2_1 << 3) | (vals[i].xdcg)); | |
549 | saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f, | |
550 | (vals[i].vpfy << 2) | vals[i].vpfy); | |
551 | } | |
552 | ||
553 | static void set_v_scale(struct saa7134_dev *dev, int task, int yscale) | |
554 | { | |
555 | int val,mirror; | |
556 | ||
557 | saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff); | |
558 | saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8); | |
559 | ||
560 | mirror = (dev->ctl_mirror) ? 0x02 : 0x00; | |
561 | if (yscale < 2048) { | |
562 | /* LPI */ | |
563 | dprintk("yscale LPI yscale=%d\n",yscale); | |
564 | saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror); | |
565 | saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40); | |
566 | saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40); | |
567 | } else { | |
568 | /* ACM */ | |
569 | val = 0x40 * 1024 / yscale; | |
570 | dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val); | |
571 | saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror); | |
572 | saa_writeb(SAA7134_LUMA_CONTRAST(task), val); | |
573 | saa_writeb(SAA7134_CHROMA_SATURATION(task), val); | |
574 | } | |
575 | saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80); | |
576 | } | |
577 | ||
578 | static void set_size(struct saa7134_dev *dev, int task, | |
579 | int width, int height, int interlace) | |
580 | { | |
581 | int prescale,xscale,yscale,y_even,y_odd; | |
582 | int h_start, h_stop, v_start, v_stop; | |
583 | int div = interlace ? 2 : 1; | |
584 | ||
585 | /* setup video scaler */ | |
586 | h_start = dev->crop_current.left; | |
587 | v_start = dev->crop_current.top/2; | |
588 | h_stop = (dev->crop_current.left + dev->crop_current.width -1); | |
589 | v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2; | |
590 | ||
591 | saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff); | |
592 | saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8); | |
593 | saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff); | |
594 | saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8); | |
595 | saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff); | |
596 | saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8); | |
597 | saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff); | |
598 | saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8); | |
599 | ||
600 | prescale = dev->crop_current.width / width; | |
601 | if (0 == prescale) | |
602 | prescale = 1; | |
603 | xscale = 1024 * dev->crop_current.width / prescale / width; | |
604 | yscale = 512 * div * dev->crop_current.height / height; | |
4ac97914 | 605 | dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale); |
1da177e4 LT |
606 | set_h_prescale(dev,task,prescale); |
607 | saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff); | |
608 | saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8); | |
609 | set_v_scale(dev,task,yscale); | |
610 | ||
611 | saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff); | |
612 | saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8); | |
613 | saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff); | |
614 | saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8); | |
615 | ||
616 | /* deinterlace y offsets */ | |
617 | y_odd = dev->ctl_y_odd; | |
618 | y_even = dev->ctl_y_even; | |
619 | saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd); | |
620 | saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even); | |
621 | saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd); | |
622 | saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even); | |
623 | } | |
624 | ||
625 | /* ------------------------------------------------------------------ */ | |
626 | ||
627 | struct cliplist { | |
628 | __u16 position; | |
629 | __u8 enable; | |
630 | __u8 disable; | |
631 | }; | |
632 | ||
1da177e4 LT |
633 | static void set_cliplist(struct saa7134_dev *dev, int reg, |
634 | struct cliplist *cl, int entries, char *name) | |
635 | { | |
636 | __u8 winbits = 0; | |
637 | int i; | |
638 | ||
639 | for (i = 0; i < entries; i++) { | |
640 | winbits |= cl[i].enable; | |
641 | winbits &= ~cl[i].disable; | |
642 | if (i < 15 && cl[i].position == cl[i+1].position) | |
643 | continue; | |
644 | saa_writeb(reg + 0, winbits); | |
645 | saa_writeb(reg + 2, cl[i].position & 0xff); | |
646 | saa_writeb(reg + 3, cl[i].position >> 8); | |
647 | dprintk("clip: %s winbits=%02x pos=%d\n", | |
648 | name,winbits,cl[i].position); | |
649 | reg += 8; | |
650 | } | |
651 | for (; reg < 0x400; reg += 8) { | |
652 | saa_writeb(reg+ 0, 0); | |
653 | saa_writeb(reg + 1, 0); | |
654 | saa_writeb(reg + 2, 0); | |
655 | saa_writeb(reg + 3, 0); | |
656 | } | |
657 | } | |
658 | ||
659 | static int clip_range(int val) | |
660 | { | |
661 | if (val < 0) | |
662 | val = 0; | |
663 | return val; | |
664 | } | |
665 | ||
9040b32e HO |
666 | /* Sort into smallest position first order */ |
667 | static int cliplist_cmp(const void *a, const void *b) | |
668 | { | |
669 | const struct cliplist *cla = a; | |
670 | const struct cliplist *clb = b; | |
671 | if (cla->position < clb->position) | |
672 | return -1; | |
673 | if (cla->position > clb->position) | |
674 | return 1; | |
675 | return 0; | |
676 | } | |
677 | ||
1da177e4 LT |
678 | static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips, |
679 | int nclips, int interlace) | |
680 | { | |
681 | struct cliplist col[16], row[16]; | |
9040b32e | 682 | int cols = 0, rows = 0, i; |
1da177e4 LT |
683 | int div = interlace ? 2 : 1; |
684 | ||
9040b32e HO |
685 | memset(col, 0, sizeof(col)); |
686 | memset(row, 0, sizeof(row)); | |
1da177e4 LT |
687 | for (i = 0; i < nclips && i < 8; i++) { |
688 | col[cols].position = clip_range(clips[i].c.left); | |
689 | col[cols].enable = (1 << i); | |
690 | cols++; | |
691 | col[cols].position = clip_range(clips[i].c.left+clips[i].c.width); | |
692 | col[cols].disable = (1 << i); | |
693 | cols++; | |
694 | row[rows].position = clip_range(clips[i].c.top / div); | |
695 | row[rows].enable = (1 << i); | |
696 | rows++; | |
697 | row[rows].position = clip_range((clips[i].c.top + clips[i].c.height) | |
698 | / div); | |
699 | row[rows].disable = (1 << i); | |
700 | rows++; | |
701 | } | |
9040b32e HO |
702 | sort(col, cols, sizeof col[0], cliplist_cmp, NULL); |
703 | sort(row, rows, sizeof row[0], cliplist_cmp, NULL); | |
1da177e4 LT |
704 | set_cliplist(dev,0x380,col,cols,"cols"); |
705 | set_cliplist(dev,0x384,row,rows,"rows"); | |
706 | return 0; | |
707 | } | |
708 | ||
3a0a5a78 | 709 | static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try) |
1da177e4 LT |
710 | { |
711 | enum v4l2_field field; | |
712 | int maxw, maxh; | |
713 | ||
3a0a5a78 | 714 | if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL)) |
1da177e4 | 715 | return -EINVAL; |
3a0a5a78 HV |
716 | if (win->w.width < 48) |
717 | win->w.width = 48; | |
718 | if (win->w.height < 32) | |
719 | win->w.height = 32; | |
720 | if (win->clipcount > 8) | |
721 | win->clipcount = 8; | |
722 | ||
723 | win->chromakey = 0; | |
724 | win->global_alpha = 0; | |
1da177e4 LT |
725 | field = win->field; |
726 | maxw = dev->crop_current.width; | |
727 | maxh = dev->crop_current.height; | |
728 | ||
729 | if (V4L2_FIELD_ANY == field) { | |
4ac97914 MCC |
730 | field = (win->w.height > maxh/2) |
731 | ? V4L2_FIELD_INTERLACED | |
732 | : V4L2_FIELD_TOP; | |
733 | } | |
734 | switch (field) { | |
735 | case V4L2_FIELD_TOP: | |
736 | case V4L2_FIELD_BOTTOM: | |
737 | maxh = maxh / 2; | |
738 | break; | |
4ac97914 | 739 | default: |
3a0a5a78 HV |
740 | field = V4L2_FIELD_INTERLACED; |
741 | break; | |
4ac97914 | 742 | } |
1da177e4 LT |
743 | |
744 | win->field = field; | |
745 | if (win->w.width > maxw) | |
746 | win->w.width = maxw; | |
747 | if (win->w.height > maxh) | |
748 | win->w.height = maxh; | |
749 | return 0; | |
750 | } | |
751 | ||
718bde1a | 752 | static int start_preview(struct saa7134_dev *dev) |
1da177e4 LT |
753 | { |
754 | unsigned long base,control,bpl; | |
755 | int err; | |
756 | ||
3a0a5a78 | 757 | err = verify_preview(dev, &dev->win, false); |
1da177e4 LT |
758 | if (0 != err) |
759 | return err; | |
760 | ||
b12262f9 | 761 | dev->ovfield = dev->win.field; |
1da177e4 | 762 | dprintk("start_preview %dx%d+%d+%d %s field=%s\n", |
b12262f9 HV |
763 | dev->win.w.width, dev->win.w.height, |
764 | dev->win.w.left, dev->win.w.top, | |
765 | dev->ovfmt->name, v4l2_field_names[dev->ovfield]); | |
1da177e4 LT |
766 | |
767 | /* setup window + clipping */ | |
b12262f9 | 768 | set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height, |
1da177e4 | 769 | V4L2_FIELD_HAS_BOTH(dev->ovfield)); |
b12262f9 | 770 | setup_clipping(dev, dev->clips, dev->nclips, |
1da177e4 LT |
771 | V4L2_FIELD_HAS_BOTH(dev->ovfield)); |
772 | if (dev->ovfmt->yuv) | |
773 | saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03); | |
774 | else | |
775 | saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01); | |
776 | saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20); | |
777 | ||
778 | /* dma: setup channel 1 (= Video Task B) */ | |
779 | base = (unsigned long)dev->ovbuf.base; | |
b12262f9 HV |
780 | base += dev->ovbuf.fmt.bytesperline * dev->win.w.top; |
781 | base += dev->ovfmt->depth/8 * dev->win.w.left; | |
1da177e4 LT |
782 | bpl = dev->ovbuf.fmt.bytesperline; |
783 | control = SAA7134_RS_CONTROL_BURST_16; | |
784 | if (dev->ovfmt->bswap) | |
785 | control |= SAA7134_RS_CONTROL_BSWAP; | |
786 | if (dev->ovfmt->wswap) | |
787 | control |= SAA7134_RS_CONTROL_WSWAP; | |
788 | if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) { | |
789 | saa_writel(SAA7134_RS_BA1(1),base); | |
790 | saa_writel(SAA7134_RS_BA2(1),base+bpl); | |
791 | saa_writel(SAA7134_RS_PITCH(1),bpl*2); | |
792 | saa_writel(SAA7134_RS_CONTROL(1),control); | |
793 | } else { | |
794 | saa_writel(SAA7134_RS_BA1(1),base); | |
795 | saa_writel(SAA7134_RS_BA2(1),base); | |
796 | saa_writel(SAA7134_RS_PITCH(1),bpl); | |
797 | saa_writel(SAA7134_RS_CONTROL(1),control); | |
798 | } | |
799 | ||
800 | /* start dma */ | |
801 | dev->ovenable = 1; | |
802 | saa7134_set_dmabits(dev); | |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
718bde1a | 807 | static int stop_preview(struct saa7134_dev *dev) |
1da177e4 LT |
808 | { |
809 | dev->ovenable = 0; | |
810 | saa7134_set_dmabits(dev); | |
811 | return 0; | |
812 | } | |
813 | ||
814 | /* ------------------------------------------------------------------ */ | |
815 | ||
816 | static int buffer_activate(struct saa7134_dev *dev, | |
817 | struct saa7134_buf *buf, | |
818 | struct saa7134_buf *next) | |
819 | { | |
820 | unsigned long base,control,bpl; | |
821 | unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */ | |
822 | ||
823 | dprintk("buffer_activate buf=%p\n",buf); | |
0fc0686e | 824 | buf->vb.state = VIDEOBUF_ACTIVE; |
1da177e4 LT |
825 | buf->top_seen = 0; |
826 | ||
827 | set_size(dev,TASK_A,buf->vb.width,buf->vb.height, | |
828 | V4L2_FIELD_HAS_BOTH(buf->vb.field)); | |
829 | if (buf->fmt->yuv) | |
830 | saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03); | |
831 | else | |
832 | saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01); | |
833 | saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm); | |
834 | ||
835 | /* DMA: setup channel 0 (= Video Task A0) */ | |
836 | base = saa7134_buffer_base(buf); | |
837 | if (buf->fmt->planar) | |
838 | bpl = buf->vb.width; | |
839 | else | |
840 | bpl = (buf->vb.width * buf->fmt->depth) / 8; | |
841 | control = SAA7134_RS_CONTROL_BURST_16 | | |
842 | SAA7134_RS_CONTROL_ME | | |
843 | (buf->pt->dma >> 12); | |
844 | if (buf->fmt->bswap) | |
845 | control |= SAA7134_RS_CONTROL_BSWAP; | |
846 | if (buf->fmt->wswap) | |
847 | control |= SAA7134_RS_CONTROL_WSWAP; | |
848 | if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) { | |
849 | /* interlaced */ | |
850 | saa_writel(SAA7134_RS_BA1(0),base); | |
851 | saa_writel(SAA7134_RS_BA2(0),base+bpl); | |
852 | saa_writel(SAA7134_RS_PITCH(0),bpl*2); | |
853 | } else { | |
854 | /* non-interlaced */ | |
855 | saa_writel(SAA7134_RS_BA1(0),base); | |
856 | saa_writel(SAA7134_RS_BA2(0),base); | |
857 | saa_writel(SAA7134_RS_PITCH(0),bpl); | |
858 | } | |
859 | saa_writel(SAA7134_RS_CONTROL(0),control); | |
860 | ||
861 | if (buf->fmt->planar) { | |
862 | /* DMA: setup channel 4+5 (= planar task A) */ | |
863 | bpl_uv = bpl >> buf->fmt->hshift; | |
864 | lines_uv = buf->vb.height >> buf->fmt->vshift; | |
865 | base2 = base + bpl * buf->vb.height; | |
866 | base3 = base2 + bpl_uv * lines_uv; | |
867 | if (buf->fmt->uvswap) | |
868 | tmp = base2, base2 = base3, base3 = tmp; | |
869 | dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n", | |
870 | bpl_uv,lines_uv,base2,base3); | |
871 | if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) { | |
872 | /* interlaced */ | |
873 | saa_writel(SAA7134_RS_BA1(4),base2); | |
874 | saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv); | |
875 | saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2); | |
876 | saa_writel(SAA7134_RS_BA1(5),base3); | |
877 | saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv); | |
878 | saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2); | |
879 | } else { | |
880 | /* non-interlaced */ | |
881 | saa_writel(SAA7134_RS_BA1(4),base2); | |
882 | saa_writel(SAA7134_RS_BA2(4),base2); | |
883 | saa_writel(SAA7134_RS_PITCH(4),bpl_uv); | |
884 | saa_writel(SAA7134_RS_BA1(5),base3); | |
885 | saa_writel(SAA7134_RS_BA2(5),base3); | |
886 | saa_writel(SAA7134_RS_PITCH(5),bpl_uv); | |
887 | } | |
888 | saa_writel(SAA7134_RS_CONTROL(4),control); | |
889 | saa_writel(SAA7134_RS_CONTROL(5),control); | |
890 | } | |
891 | ||
892 | /* start DMA */ | |
893 | saa7134_set_dmabits(dev); | |
894 | mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT); | |
895 | return 0; | |
896 | } | |
897 | ||
898 | static int buffer_prepare(struct videobuf_queue *q, | |
899 | struct videobuf_buffer *vb, | |
900 | enum v4l2_field field) | |
901 | { | |
9db0fb18 | 902 | struct saa7134_dev *dev = q->priv_data; |
1da177e4 LT |
903 | struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); |
904 | unsigned int size; | |
905 | int err; | |
906 | ||
907 | /* sanity checks */ | |
813b9dff | 908 | if (NULL == dev->fmt) |
1da177e4 | 909 | return -EINVAL; |
813b9dff HV |
910 | if (dev->width < 48 || |
911 | dev->height < 32 || | |
912 | dev->width/4 > dev->crop_current.width || | |
913 | dev->height/4 > dev->crop_current.height || | |
914 | dev->width > dev->crop_bounds.width || | |
915 | dev->height > dev->crop_bounds.height) | |
1da177e4 | 916 | return -EINVAL; |
813b9dff | 917 | size = (dev->width * dev->height * dev->fmt->depth) >> 3; |
1da177e4 LT |
918 | if (0 != buf->vb.baddr && buf->vb.bsize < size) |
919 | return -EINVAL; | |
920 | ||
921 | dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n", | |
813b9dff HV |
922 | vb->i, dev->width, dev->height, size, v4l2_field_names[field], |
923 | dev->fmt->name); | |
924 | if (buf->vb.width != dev->width || | |
925 | buf->vb.height != dev->height || | |
1da177e4 LT |
926 | buf->vb.size != size || |
927 | buf->vb.field != field || | |
813b9dff | 928 | buf->fmt != dev->fmt) { |
c7b0ac05 | 929 | saa7134_dma_free(q,buf); |
1da177e4 LT |
930 | } |
931 | ||
0fc0686e | 932 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
c1accaa2 MCC |
933 | struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); |
934 | ||
813b9dff HV |
935 | buf->vb.width = dev->width; |
936 | buf->vb.height = dev->height; | |
1da177e4 LT |
937 | buf->vb.size = size; |
938 | buf->vb.field = field; | |
813b9dff | 939 | buf->fmt = dev->fmt; |
9db0fb18 | 940 | buf->pt = &dev->pt_cap; |
d5709a0e | 941 | dev->video_q.curr = NULL; |
1da177e4 | 942 | |
c7b0ac05 | 943 | err = videobuf_iolock(q,&buf->vb,&dev->ovbuf); |
1da177e4 LT |
944 | if (err) |
945 | goto oops; | |
946 | err = saa7134_pgtable_build(dev->pci,buf->pt, | |
c1accaa2 MCC |
947 | dma->sglist, |
948 | dma->sglen, | |
1da177e4 LT |
949 | saa7134_buffer_startpage(buf)); |
950 | if (err) | |
951 | goto oops; | |
952 | } | |
0fc0686e | 953 | buf->vb.state = VIDEOBUF_PREPARED; |
1da177e4 LT |
954 | buf->activate = buffer_activate; |
955 | return 0; | |
956 | ||
957 | oops: | |
c7b0ac05 | 958 | saa7134_dma_free(q,buf); |
1da177e4 LT |
959 | return err; |
960 | } | |
961 | ||
962 | static int | |
963 | buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) | |
964 | { | |
9db0fb18 | 965 | struct saa7134_dev *dev = q->priv_data; |
1da177e4 | 966 | |
813b9dff | 967 | *size = dev->fmt->depth * dev->width * dev->height >> 3; |
1da177e4 LT |
968 | if (0 == *count) |
969 | *count = gbuffers; | |
970 | *count = saa7134_buffer_count(*size,*count); | |
971 | return 0; | |
972 | } | |
973 | ||
974 | static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) | |
975 | { | |
9db0fb18 | 976 | struct saa7134_dev *dev = q->priv_data; |
1da177e4 LT |
977 | struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); |
978 | ||
9db0fb18 | 979 | saa7134_buffer_queue(dev, &dev->video_q, buf); |
1da177e4 LT |
980 | } |
981 | ||
982 | static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) | |
983 | { | |
1da177e4 LT |
984 | struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb); |
985 | ||
c7b0ac05 | 986 | saa7134_dma_free(q,buf); |
1da177e4 LT |
987 | } |
988 | ||
989 | static struct videobuf_queue_ops video_qops = { | |
990 | .buf_setup = buffer_setup, | |
991 | .buf_prepare = buffer_prepare, | |
992 | .buf_queue = buffer_queue, | |
993 | .buf_release = buffer_release, | |
994 | }; | |
995 | ||
996 | /* ------------------------------------------------------------------ */ | |
997 | ||
718bde1a | 998 | static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl) |
531d83a3 | 999 | { |
718bde1a | 1000 | struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler); |
1da177e4 LT |
1001 | unsigned long flags; |
1002 | int restart_overlay = 0; | |
1003 | ||
718bde1a | 1004 | switch (ctrl->id) { |
1da177e4 | 1005 | case V4L2_CID_BRIGHTNESS: |
718bde1a HV |
1006 | dev->ctl_bright = ctrl->val; |
1007 | saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val); | |
1da177e4 LT |
1008 | break; |
1009 | case V4L2_CID_HUE: | |
718bde1a HV |
1010 | dev->ctl_hue = ctrl->val; |
1011 | saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val); | |
1da177e4 LT |
1012 | break; |
1013 | case V4L2_CID_CONTRAST: | |
718bde1a | 1014 | dev->ctl_contrast = ctrl->val; |
1da177e4 LT |
1015 | saa_writeb(SAA7134_DEC_LUMA_CONTRAST, |
1016 | dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast); | |
1017 | break; | |
1018 | case V4L2_CID_SATURATION: | |
718bde1a | 1019 | dev->ctl_saturation = ctrl->val; |
1da177e4 LT |
1020 | saa_writeb(SAA7134_DEC_CHROMA_SATURATION, |
1021 | dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation); | |
1022 | break; | |
1023 | case V4L2_CID_AUDIO_MUTE: | |
718bde1a | 1024 | dev->ctl_mute = ctrl->val; |
1da177e4 LT |
1025 | saa7134_tvaudio_setmute(dev); |
1026 | break; | |
1027 | case V4L2_CID_AUDIO_VOLUME: | |
718bde1a | 1028 | dev->ctl_volume = ctrl->val; |
1da177e4 LT |
1029 | saa7134_tvaudio_setvolume(dev,dev->ctl_volume); |
1030 | break; | |
1031 | case V4L2_CID_PRIVATE_INVERT: | |
718bde1a | 1032 | dev->ctl_invert = ctrl->val; |
1da177e4 LT |
1033 | saa_writeb(SAA7134_DEC_LUMA_CONTRAST, |
1034 | dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast); | |
1035 | saa_writeb(SAA7134_DEC_CHROMA_SATURATION, | |
1036 | dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation); | |
1037 | break; | |
f246a817 | 1038 | case V4L2_CID_HFLIP: |
718bde1a | 1039 | dev->ctl_mirror = ctrl->val; |
1da177e4 LT |
1040 | restart_overlay = 1; |
1041 | break; | |
1042 | case V4L2_CID_PRIVATE_Y_EVEN: | |
718bde1a | 1043 | dev->ctl_y_even = ctrl->val; |
1da177e4 LT |
1044 | restart_overlay = 1; |
1045 | break; | |
1046 | case V4L2_CID_PRIVATE_Y_ODD: | |
718bde1a | 1047 | dev->ctl_y_odd = ctrl->val; |
1da177e4 LT |
1048 | restart_overlay = 1; |
1049 | break; | |
1050 | case V4L2_CID_PRIVATE_AUTOMUTE: | |
7f171123 MCC |
1051 | { |
1052 | struct v4l2_priv_tun_config tda9887_cfg; | |
1053 | ||
1054 | tda9887_cfg.tuner = TUNER_TDA9887; | |
1055 | tda9887_cfg.priv = &dev->tda9887_conf; | |
1056 | ||
718bde1a | 1057 | dev->ctl_automute = ctrl->val; |
1da177e4 LT |
1058 | if (dev->tda9887_conf) { |
1059 | if (dev->ctl_automute) | |
1060 | dev->tda9887_conf |= TDA9887_AUTOMUTE; | |
1061 | else | |
1062 | dev->tda9887_conf &= ~TDA9887_AUTOMUTE; | |
7f171123 | 1063 | |
fac6986c | 1064 | saa_call_all(dev, tuner, s_config, &tda9887_cfg); |
1da177e4 LT |
1065 | } |
1066 | break; | |
7f171123 | 1067 | } |
1da177e4 | 1068 | default: |
718bde1a | 1069 | return -EINVAL; |
1da177e4 | 1070 | } |
718bde1a HV |
1071 | if (restart_overlay && res_locked(dev, RESOURCE_OVERLAY)) { |
1072 | spin_lock_irqsave(&dev->slock, flags); | |
1073 | stop_preview(dev); | |
1074 | start_preview(dev); | |
1075 | spin_unlock_irqrestore(&dev->slock, flags); | |
1da177e4 | 1076 | } |
718bde1a | 1077 | return 0; |
531d83a3 | 1078 | } |
1da177e4 LT |
1079 | |
1080 | /* ------------------------------------------------------------------ */ | |
1081 | ||
8fcd4769 | 1082 | static struct videobuf_queue *saa7134_queue(struct file *file) |
1da177e4 | 1083 | { |
8fcd4769 | 1084 | struct video_device *vdev = video_devdata(file); |
b9f63b25 | 1085 | struct saa7134_dev *dev = video_drvdata(file); |
ce791139 | 1086 | struct saa7134_fh *fh = file->private_data; |
8fcd4769 | 1087 | struct videobuf_queue *q = NULL; |
1da177e4 | 1088 | |
8fcd4769 HV |
1089 | switch (vdev->vfl_type) { |
1090 | case VFL_TYPE_GRABBER: | |
ce791139 | 1091 | q = fh->is_empress ? &dev->empress_tsq : &dev->cap; |
1da177e4 | 1092 | break; |
8fcd4769 | 1093 | case VFL_TYPE_VBI: |
9db0fb18 | 1094 | q = &dev->vbi; |
1da177e4 LT |
1095 | break; |
1096 | default: | |
1097 | BUG(); | |
1098 | } | |
1099 | return q; | |
1100 | } | |
1101 | ||
8fcd4769 | 1102 | static int saa7134_resource(struct file *file) |
1da177e4 | 1103 | { |
8fcd4769 | 1104 | struct video_device *vdev = video_devdata(file); |
ce791139 | 1105 | struct saa7134_fh *fh = file->private_data; |
8fcd4769 HV |
1106 | |
1107 | if (vdev->vfl_type == VFL_TYPE_GRABBER) | |
ce791139 | 1108 | return fh->is_empress ? RESOURCE_EMPRESS : RESOURCE_VIDEO; |
1da177e4 | 1109 | |
8fcd4769 | 1110 | if (vdev->vfl_type == VFL_TYPE_VBI) |
9040b32e HO |
1111 | return RESOURCE_VBI; |
1112 | ||
1113 | BUG(); | |
1114 | return 0; | |
1da177e4 LT |
1115 | } |
1116 | ||
bec43661 | 1117 | static int video_open(struct file *file) |
1da177e4 | 1118 | { |
63b0d5ad LP |
1119 | struct video_device *vdev = video_devdata(file); |
1120 | struct saa7134_dev *dev = video_drvdata(file); | |
1da177e4 | 1121 | struct saa7134_fh *fh; |
1da177e4 LT |
1122 | |
1123 | /* allocate + initialize per filehandle data */ | |
7408187d | 1124 | fh = kzalloc(sizeof(*fh),GFP_KERNEL); |
befd6e64 | 1125 | if (NULL == fh) |
1da177e4 | 1126 | return -ENOMEM; |
befd6e64 | 1127 | |
3bbaa3a6 | 1128 | v4l2_fh_init(&fh->fh, vdev); |
1da177e4 | 1129 | file->private_data = fh; |
1da177e4 | 1130 | |
8fcd4769 | 1131 | if (vdev->vfl_type == VFL_TYPE_RADIO) { |
1da177e4 LT |
1132 | /* switch to radio mode */ |
1133 | saa7134_tvaudio_setinput(dev,&card(dev).radio); | |
fac6986c | 1134 | saa_call_all(dev, tuner, s_radio); |
1da177e4 LT |
1135 | } else { |
1136 | /* switch to video/vbi mode */ | |
1137 | video_mux(dev,dev->ctl_input); | |
1138 | } | |
3bbaa3a6 OZ |
1139 | v4l2_fh_add(&fh->fh); |
1140 | ||
330a115a | 1141 | return 0; |
1da177e4 LT |
1142 | } |
1143 | ||
1144 | static ssize_t | |
1145 | video_read(struct file *file, char __user *data, size_t count, loff_t *ppos) | |
1146 | { | |
8fcd4769 | 1147 | struct video_device *vdev = video_devdata(file); |
b9f63b25 | 1148 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 LT |
1149 | struct saa7134_fh *fh = file->private_data; |
1150 | ||
8fcd4769 HV |
1151 | switch (vdev->vfl_type) { |
1152 | case VFL_TYPE_GRABBER: | |
b9f63b25 | 1153 | if (res_locked(dev, RESOURCE_VIDEO)) |
1da177e4 | 1154 | return -EBUSY; |
8fcd4769 | 1155 | return videobuf_read_one(saa7134_queue(file), |
1da177e4 LT |
1156 | data, count, ppos, |
1157 | file->f_flags & O_NONBLOCK); | |
8fcd4769 | 1158 | case VFL_TYPE_VBI: |
b9f63b25 | 1159 | if (!res_get(dev, fh, RESOURCE_VBI)) |
1da177e4 | 1160 | return -EBUSY; |
8fcd4769 | 1161 | return videobuf_read_stream(saa7134_queue(file), |
1da177e4 LT |
1162 | data, count, ppos, 1, |
1163 | file->f_flags & O_NONBLOCK); | |
1164 | break; | |
1165 | default: | |
1166 | BUG(); | |
1167 | return 0; | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | static unsigned int | |
1172 | video_poll(struct file *file, struct poll_table_struct *wait) | |
1173 | { | |
a2004502 | 1174 | unsigned long req_events = poll_requested_events(wait); |
8fcd4769 | 1175 | struct video_device *vdev = video_devdata(file); |
b9f63b25 | 1176 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 LT |
1177 | struct saa7134_fh *fh = file->private_data; |
1178 | struct videobuf_buffer *buf = NULL; | |
4a567394 | 1179 | unsigned int rc = 0; |
1da177e4 | 1180 | |
a2004502 HV |
1181 | if (v4l2_event_pending(&fh->fh)) |
1182 | rc = POLLPRI; | |
1183 | else if (req_events & POLLPRI) | |
1184 | poll_wait(file, &fh->fh.wait, wait); | |
1185 | ||
8fcd4769 | 1186 | if (vdev->vfl_type == VFL_TYPE_VBI) |
a2004502 | 1187 | return rc | videobuf_poll_stream(file, &dev->vbi, wait); |
1da177e4 | 1188 | |
b9f63b25 | 1189 | if (res_check(fh, RESOURCE_VIDEO)) { |
9db0fb18 HV |
1190 | mutex_lock(&dev->cap.vb_lock); |
1191 | if (!list_empty(&dev->cap.stream)) | |
1192 | buf = list_entry(dev->cap.stream.next, struct videobuf_buffer, stream); | |
1da177e4 | 1193 | } else { |
9db0fb18 HV |
1194 | mutex_lock(&dev->cap.vb_lock); |
1195 | if (UNSET == dev->cap.read_off) { | |
330a115a | 1196 | /* need to capture a new frame */ |
b9f63b25 | 1197 | if (res_locked(dev, RESOURCE_VIDEO)) |
64f9477f | 1198 | goto err; |
9db0fb18 HV |
1199 | if (0 != dev->cap.ops->buf_prepare(&dev->cap, |
1200 | dev->cap.read_buf, dev->cap.field)) | |
64f9477f | 1201 | goto err; |
9db0fb18 HV |
1202 | dev->cap.ops->buf_queue(&dev->cap, dev->cap.read_buf); |
1203 | dev->cap.read_off = 0; | |
1da177e4 | 1204 | } |
9db0fb18 | 1205 | buf = dev->cap.read_buf; |
1da177e4 LT |
1206 | } |
1207 | ||
1208 | if (!buf) | |
4a567394 | 1209 | goto err; |
1da177e4 LT |
1210 | |
1211 | poll_wait(file, &buf->done, wait); | |
a2004502 HV |
1212 | if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) |
1213 | rc |= POLLIN | POLLRDNORM; | |
9db0fb18 | 1214 | mutex_unlock(&dev->cap.vb_lock); |
4a567394 | 1215 | return rc; |
64f9477f MCC |
1216 | |
1217 | err: | |
9db0fb18 | 1218 | mutex_unlock(&dev->cap.vb_lock); |
a2004502 | 1219 | return rc | POLLERR; |
1da177e4 LT |
1220 | } |
1221 | ||
bec43661 | 1222 | static int video_release(struct file *file) |
1da177e4 | 1223 | { |
8fcd4769 | 1224 | struct video_device *vdev = video_devdata(file); |
b9f63b25 HV |
1225 | struct saa7134_dev *dev = video_drvdata(file); |
1226 | struct saa7134_fh *fh = file->private_data; | |
b9218f2f | 1227 | struct saa6588_command cmd; |
1da177e4 LT |
1228 | unsigned long flags; |
1229 | ||
a1dca1e3 SS |
1230 | saa7134_tvaudio_close(dev); |
1231 | ||
1da177e4 LT |
1232 | /* turn off overlay */ |
1233 | if (res_check(fh, RESOURCE_OVERLAY)) { | |
1234 | spin_lock_irqsave(&dev->slock,flags); | |
718bde1a | 1235 | stop_preview(dev); |
1da177e4 | 1236 | spin_unlock_irqrestore(&dev->slock,flags); |
b9f63b25 | 1237 | res_free(dev, fh, RESOURCE_OVERLAY); |
1da177e4 LT |
1238 | } |
1239 | ||
1240 | /* stop video capture */ | |
1241 | if (res_check(fh, RESOURCE_VIDEO)) { | |
9c9cff55 | 1242 | pm_qos_remove_request(&dev->qos_request); |
9db0fb18 | 1243 | videobuf_streamoff(&dev->cap); |
b9f63b25 | 1244 | res_free(dev, fh, RESOURCE_VIDEO); |
9db0fb18 | 1245 | videobuf_mmap_free(&dev->cap); |
1da177e4 | 1246 | } |
9db0fb18 HV |
1247 | if (dev->cap.read_buf) { |
1248 | buffer_release(&dev->cap, dev->cap.read_buf); | |
1249 | kfree(dev->cap.read_buf); | |
1da177e4 LT |
1250 | } |
1251 | ||
1252 | /* stop vbi capture */ | |
1253 | if (res_check(fh, RESOURCE_VBI)) { | |
9db0fb18 | 1254 | videobuf_stop(&dev->vbi); |
b9f63b25 | 1255 | res_free(dev, fh, RESOURCE_VBI); |
9db0fb18 | 1256 | videobuf_mmap_free(&dev->vbi); |
1da177e4 LT |
1257 | } |
1258 | ||
330a115a MCC |
1259 | /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/ |
1260 | saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0); | |
1261 | saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0); | |
1262 | saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0); | |
1263 | saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0); | |
1264 | ||
622b828a | 1265 | saa_call_all(dev, core, s_power, 0); |
8fcd4769 | 1266 | if (vdev->vfl_type == VFL_TYPE_RADIO) |
b9218f2f | 1267 | saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd); |
330a115a | 1268 | |
3bbaa3a6 OZ |
1269 | v4l2_fh_del(&fh->fh); |
1270 | v4l2_fh_exit(&fh->fh); | |
1da177e4 LT |
1271 | file->private_data = NULL; |
1272 | kfree(fh); | |
1273 | return 0; | |
1274 | } | |
1275 | ||
9040b32e | 1276 | static int video_mmap(struct file *file, struct vm_area_struct * vma) |
1da177e4 | 1277 | { |
8fcd4769 | 1278 | return videobuf_mmap_mapper(saa7134_queue(file), vma); |
1da177e4 LT |
1279 | } |
1280 | ||
2983baf8 HV |
1281 | static ssize_t radio_read(struct file *file, char __user *data, |
1282 | size_t count, loff_t *ppos) | |
1283 | { | |
b9f63b25 | 1284 | struct saa7134_dev *dev = video_drvdata(file); |
b9218f2f | 1285 | struct saa6588_command cmd; |
2983baf8 HV |
1286 | |
1287 | cmd.block_count = count/3; | |
1288 | cmd.buffer = data; | |
1289 | cmd.instance = file; | |
1290 | cmd.result = -ENODEV; | |
1291 | ||
b9218f2f | 1292 | saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd); |
2983baf8 HV |
1293 | |
1294 | return cmd.result; | |
1295 | } | |
1296 | ||
1297 | static unsigned int radio_poll(struct file *file, poll_table *wait) | |
1298 | { | |
b9f63b25 | 1299 | struct saa7134_dev *dev = video_drvdata(file); |
b9218f2f | 1300 | struct saa6588_command cmd; |
a2004502 | 1301 | unsigned int rc = v4l2_ctrl_poll(file, wait); |
2983baf8 HV |
1302 | |
1303 | cmd.instance = file; | |
1304 | cmd.event_list = wait; | |
a2004502 | 1305 | cmd.result = 0; |
b9218f2f | 1306 | saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd); |
2983baf8 | 1307 | |
a2004502 | 1308 | return rc | cmd.result; |
2983baf8 HV |
1309 | } |
1310 | ||
1da177e4 LT |
1311 | /* ------------------------------------------------------------------ */ |
1312 | ||
78b526a4 | 1313 | static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv, |
8c85454a | 1314 | struct v4l2_format *f) |
1da177e4 | 1315 | { |
b9f63b25 | 1316 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 LT |
1317 | struct saa7134_tvnorm *norm = dev->tvnorm; |
1318 | ||
d047795c | 1319 | memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved)); |
1da177e4 LT |
1320 | f->fmt.vbi.sampling_rate = 6750000 * 4; |
1321 | f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */; | |
1322 | f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; | |
1323 | f->fmt.vbi.offset = 64 * 4; | |
f246a817 MS |
1324 | f->fmt.vbi.start[0] = norm->vbi_v_start_0; |
1325 | f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1; | |
1326 | f->fmt.vbi.start[1] = norm->vbi_v_start_1; | |
1da177e4 LT |
1327 | f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; |
1328 | f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */ | |
1329 | ||
8c85454a | 1330 | return 0; |
1da177e4 LT |
1331 | } |
1332 | ||
78b526a4 | 1333 | static int saa7134_g_fmt_vid_cap(struct file *file, void *priv, |
8f61ae2f | 1334 | struct v4l2_format *f) |
1da177e4 | 1335 | { |
b9f63b25 | 1336 | struct saa7134_dev *dev = video_drvdata(file); |
8f61ae2f | 1337 | |
813b9dff HV |
1338 | f->fmt.pix.width = dev->width; |
1339 | f->fmt.pix.height = dev->height; | |
9db0fb18 | 1340 | f->fmt.pix.field = dev->cap.field; |
813b9dff | 1341 | f->fmt.pix.pixelformat = dev->fmt->fourcc; |
8c85454a | 1342 | f->fmt.pix.bytesperline = |
813b9dff | 1343 | (f->fmt.pix.width * dev->fmt->depth) >> 3; |
8c85454a MCC |
1344 | f->fmt.pix.sizeimage = |
1345 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
3a0a5a78 HV |
1346 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
1347 | f->fmt.pix.priv = 0; | |
8c85454a MCC |
1348 | return 0; |
1349 | } | |
1350 | ||
78b526a4 | 1351 | static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv, |
8c85454a MCC |
1352 | struct v4l2_format *f) |
1353 | { | |
b9f63b25 | 1354 | struct saa7134_dev *dev = video_drvdata(file); |
12d866ec | 1355 | struct v4l2_clip __user *clips = f->fmt.win.clips; |
3a0a5a78 HV |
1356 | u32 clipcount = f->fmt.win.clipcount; |
1357 | int err = 0; | |
1358 | int i; | |
8c85454a MCC |
1359 | |
1360 | if (saa7134_no_overlay > 0) { | |
1361 | printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); | |
1da177e4 LT |
1362 | return -EINVAL; |
1363 | } | |
3a0a5a78 | 1364 | mutex_lock(&dev->lock); |
b12262f9 | 1365 | f->fmt.win = dev->win; |
3a0a5a78 HV |
1366 | f->fmt.win.clips = clips; |
1367 | if (clips == NULL) | |
1368 | clipcount = 0; | |
1369 | if (dev->nclips < clipcount) | |
1370 | clipcount = dev->nclips; | |
1371 | f->fmt.win.clipcount = clipcount; | |
1372 | ||
1373 | for (i = 0; !err && i < clipcount; i++) { | |
1374 | if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c, | |
1375 | sizeof(struct v4l2_rect))) | |
1376 | err = -EFAULT; | |
1377 | } | |
1378 | mutex_unlock(&dev->lock); | |
8c85454a | 1379 | |
3a0a5a78 | 1380 | return err; |
1da177e4 LT |
1381 | } |
1382 | ||
78b526a4 | 1383 | static int saa7134_try_fmt_vid_cap(struct file *file, void *priv, |
8f61ae2f | 1384 | struct v4l2_format *f) |
1da177e4 | 1385 | { |
b9f63b25 | 1386 | struct saa7134_dev *dev = video_drvdata(file); |
8c85454a MCC |
1387 | struct saa7134_format *fmt; |
1388 | enum v4l2_field field; | |
1389 | unsigned int maxw, maxh; | |
1da177e4 | 1390 | |
8c85454a MCC |
1391 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
1392 | if (NULL == fmt) | |
1393 | return -EINVAL; | |
1da177e4 | 1394 | |
8c85454a MCC |
1395 | field = f->fmt.pix.field; |
1396 | maxw = min(dev->crop_current.width*4, dev->crop_bounds.width); | |
1397 | maxh = min(dev->crop_current.height*4, dev->crop_bounds.height); | |
1da177e4 | 1398 | |
8c85454a MCC |
1399 | if (V4L2_FIELD_ANY == field) { |
1400 | field = (f->fmt.pix.height > maxh/2) | |
1401 | ? V4L2_FIELD_INTERLACED | |
1402 | : V4L2_FIELD_BOTTOM; | |
1403 | } | |
1404 | switch (field) { | |
1405 | case V4L2_FIELD_TOP: | |
1406 | case V4L2_FIELD_BOTTOM: | |
1407 | maxh = maxh / 2; | |
1408 | break; | |
8c85454a | 1409 | default: |
3a0a5a78 HV |
1410 | field = V4L2_FIELD_INTERLACED; |
1411 | break; | |
8c85454a | 1412 | } |
1da177e4 | 1413 | |
8c85454a | 1414 | f->fmt.pix.field = field; |
0a062033 HRK |
1415 | if (f->fmt.pix.width < 48) |
1416 | f->fmt.pix.width = 48; | |
1417 | if (f->fmt.pix.height < 32) | |
1418 | f->fmt.pix.height = 32; | |
1419 | if (f->fmt.pix.width > maxw) | |
1420 | f->fmt.pix.width = maxw; | |
1421 | if (f->fmt.pix.height > maxh) | |
1422 | f->fmt.pix.height = maxh; | |
1423 | f->fmt.pix.width &= ~0x03; | |
8c85454a MCC |
1424 | f->fmt.pix.bytesperline = |
1425 | (f->fmt.pix.width * fmt->depth) >> 3; | |
1426 | f->fmt.pix.sizeimage = | |
1427 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
3a0a5a78 HV |
1428 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
1429 | f->fmt.pix.priv = 0; | |
1da177e4 | 1430 | |
8c85454a MCC |
1431 | return 0; |
1432 | } | |
1da177e4 | 1433 | |
78b526a4 | 1434 | static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv, |
8c85454a MCC |
1435 | struct v4l2_format *f) |
1436 | { | |
b9f63b25 | 1437 | struct saa7134_dev *dev = video_drvdata(file); |
8c85454a MCC |
1438 | |
1439 | if (saa7134_no_overlay > 0) { | |
1440 | printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); | |
1da177e4 LT |
1441 | return -EINVAL; |
1442 | } | |
8c85454a | 1443 | |
3a0a5a78 HV |
1444 | if (f->fmt.win.clips == NULL) |
1445 | f->fmt.win.clipcount = 0; | |
1446 | return verify_preview(dev, &f->fmt.win, true); | |
1da177e4 LT |
1447 | } |
1448 | ||
78b526a4 | 1449 | static int saa7134_s_fmt_vid_cap(struct file *file, void *priv, |
8f61ae2f | 1450 | struct v4l2_format *f) |
8c85454a | 1451 | { |
b9f63b25 | 1452 | struct saa7134_dev *dev = video_drvdata(file); |
8c85454a MCC |
1453 | int err; |
1454 | ||
78b526a4 | 1455 | err = saa7134_try_fmt_vid_cap(file, priv, f); |
8c85454a MCC |
1456 | if (0 != err) |
1457 | return err; | |
1458 | ||
813b9dff HV |
1459 | dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
1460 | dev->width = f->fmt.pix.width; | |
1461 | dev->height = f->fmt.pix.height; | |
9db0fb18 | 1462 | dev->cap.field = f->fmt.pix.field; |
8c85454a MCC |
1463 | return 0; |
1464 | } | |
1465 | ||
78b526a4 | 1466 | static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv, |
8c85454a | 1467 | struct v4l2_format *f) |
1da177e4 | 1468 | { |
b9f63b25 | 1469 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 1470 | int err; |
a8b1ecf3 | 1471 | unsigned long flags; |
1da177e4 | 1472 | |
8c85454a MCC |
1473 | if (saa7134_no_overlay > 0) { |
1474 | printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); | |
1475 | return -EINVAL; | |
1476 | } | |
3a0a5a78 HV |
1477 | if (f->fmt.win.clips == NULL) |
1478 | f->fmt.win.clipcount = 0; | |
1479 | err = verify_preview(dev, &f->fmt.win, true); | |
8c85454a MCC |
1480 | if (0 != err) |
1481 | return err; | |
1da177e4 | 1482 | |
8c85454a MCC |
1483 | mutex_lock(&dev->lock); |
1484 | ||
b12262f9 HV |
1485 | dev->win = f->fmt.win; |
1486 | dev->nclips = f->fmt.win.clipcount; | |
8c85454a | 1487 | |
b12262f9 HV |
1488 | if (copy_from_user(dev->clips, f->fmt.win.clips, |
1489 | sizeof(struct v4l2_clip) * dev->nclips)) { | |
3593cab5 | 1490 | mutex_unlock(&dev->lock); |
8c85454a | 1491 | return -EFAULT; |
1da177e4 | 1492 | } |
8c85454a | 1493 | |
b9f63b25 | 1494 | if (res_check(priv, RESOURCE_OVERLAY)) { |
8c85454a | 1495 | spin_lock_irqsave(&dev->slock, flags); |
718bde1a HV |
1496 | stop_preview(dev); |
1497 | start_preview(dev); | |
8c85454a MCC |
1498 | spin_unlock_irqrestore(&dev->slock, flags); |
1499 | } | |
1500 | ||
1501 | mutex_unlock(&dev->lock); | |
1502 | return 0; | |
1da177e4 LT |
1503 | } |
1504 | ||
b93a18d5 | 1505 | int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i) |
1da177e4 | 1506 | { |
b93a18d5 | 1507 | struct saa7134_dev *dev = video_drvdata(file); |
8f61ae2f | 1508 | unsigned int n; |
1da177e4 | 1509 | |
8f61ae2f DSL |
1510 | n = i->index; |
1511 | if (n >= SAA7134_INPUT_MAX) | |
1512 | return -EINVAL; | |
1513 | if (NULL == card_in(dev, i->index).name) | |
1514 | return -EINVAL; | |
8f61ae2f DSL |
1515 | i->index = n; |
1516 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
1517 | strcpy(i->name, card_in(dev, n).name); | |
1518 | if (card_in(dev, n).tv) | |
1519 | i->type = V4L2_INPUT_TYPE_TUNER; | |
8f61ae2f DSL |
1520 | if (n == dev->ctl_input) { |
1521 | int v1 = saa_readb(SAA7134_STATUS_VIDEO1); | |
1522 | int v2 = saa_readb(SAA7134_STATUS_VIDEO2); | |
1523 | ||
1524 | if (0 != (v1 & 0x40)) | |
1525 | i->status |= V4L2_IN_ST_NO_H_LOCK; | |
1526 | if (0 != (v2 & 0x40)) | |
95075dd0 | 1527 | i->status |= V4L2_IN_ST_NO_SIGNAL; |
8f61ae2f DSL |
1528 | if (0 != (v2 & 0x0e)) |
1529 | i->status |= V4L2_IN_ST_MACROVISION; | |
1530 | } | |
8c85454a | 1531 | i->std = SAA7134_NORMS; |
8f61ae2f DSL |
1532 | return 0; |
1533 | } | |
b93a18d5 | 1534 | EXPORT_SYMBOL_GPL(saa7134_enum_input); |
1da177e4 | 1535 | |
b93a18d5 | 1536 | int saa7134_g_input(struct file *file, void *priv, unsigned int *i) |
90bdc145 | 1537 | { |
b93a18d5 | 1538 | struct saa7134_dev *dev = video_drvdata(file); |
90bdc145 MCC |
1539 | |
1540 | *i = dev->ctl_input; | |
1541 | return 0; | |
1542 | } | |
b93a18d5 | 1543 | EXPORT_SYMBOL_GPL(saa7134_g_input); |
90bdc145 | 1544 | |
b93a18d5 | 1545 | int saa7134_s_input(struct file *file, void *priv, unsigned int i) |
90bdc145 | 1546 | { |
b93a18d5 | 1547 | struct saa7134_dev *dev = video_drvdata(file); |
90bdc145 | 1548 | |
de81c3c3 | 1549 | if (i >= SAA7134_INPUT_MAX) |
90bdc145 MCC |
1550 | return -EINVAL; |
1551 | if (NULL == card_in(dev, i).name) | |
1552 | return -EINVAL; | |
1553 | mutex_lock(&dev->lock); | |
1554 | video_mux(dev, i); | |
1555 | mutex_unlock(&dev->lock); | |
1556 | return 0; | |
1557 | } | |
b93a18d5 | 1558 | EXPORT_SYMBOL_GPL(saa7134_s_input); |
90bdc145 | 1559 | |
b93a18d5 | 1560 | int saa7134_querycap(struct file *file, void *priv, |
8f61ae2f DSL |
1561 | struct v4l2_capability *cap) |
1562 | { | |
b93a18d5 | 1563 | struct saa7134_dev *dev = video_drvdata(file); |
c3b3e0c5 | 1564 | struct video_device *vdev = video_devdata(file); |
b9f63b25 | 1565 | struct saa7134_fh *fh = priv; |
c3b3e0c5 | 1566 | u32 radio_caps, video_caps, vbi_caps; |
1da177e4 | 1567 | |
8f61ae2f DSL |
1568 | unsigned int tuner_type = dev->tuner_type; |
1569 | ||
8f61ae2f DSL |
1570 | strcpy(cap->driver, "saa7134"); |
1571 | strlcpy(cap->card, saa7134_boards[dev->board].name, | |
1572 | sizeof(cap->card)); | |
1573 | sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci)); | |
c3b3e0c5 OZ |
1574 | |
1575 | cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
1576 | if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET)) | |
1577 | cap->device_caps |= V4L2_CAP_TUNER; | |
1578 | ||
1579 | radio_caps = V4L2_CAP_RADIO; | |
f101a2a7 | 1580 | if (dev->has_rds) |
c3b3e0c5 OZ |
1581 | radio_caps |= V4L2_CAP_RDS_CAPTURE; |
1582 | ||
1583 | video_caps = V4L2_CAP_VIDEO_CAPTURE; | |
b9f63b25 | 1584 | if (saa7134_no_overlay <= 0 && !fh->is_empress) |
c3b3e0c5 OZ |
1585 | video_caps |= V4L2_CAP_VIDEO_OVERLAY; |
1586 | ||
1587 | vbi_caps = V4L2_CAP_VBI_CAPTURE; | |
1588 | ||
1589 | switch (vdev->vfl_type) { | |
1590 | case VFL_TYPE_RADIO: | |
1591 | cap->device_caps |= radio_caps; | |
1592 | break; | |
1593 | case VFL_TYPE_GRABBER: | |
1594 | cap->device_caps |= video_caps; | |
1595 | break; | |
1596 | case VFL_TYPE_VBI: | |
1597 | cap->device_caps |= vbi_caps; | |
1598 | break; | |
1599 | } | |
1600 | cap->capabilities = radio_caps | video_caps | vbi_caps | | |
1601 | cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
1602 | if (vdev->vfl_type == VFL_TYPE_RADIO) { | |
1603 | cap->device_caps &= ~V4L2_CAP_STREAMING; | |
1604 | if (!dev->has_rds) | |
1605 | cap->device_caps &= ~V4L2_CAP_READWRITE; | |
1606 | } | |
8f61ae2f | 1607 | |
473d8024 | 1608 | return 0; |
8f61ae2f | 1609 | } |
b93a18d5 | 1610 | EXPORT_SYMBOL_GPL(saa7134_querycap); |
bb881f14 | 1611 | |
b93a18d5 | 1612 | int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id) |
8f61ae2f | 1613 | { |
b93a18d5 | 1614 | struct saa7134_dev *dev = video_drvdata(file); |
b9f63b25 | 1615 | struct saa7134_fh *fh = priv; |
8f61ae2f DSL |
1616 | unsigned long flags; |
1617 | unsigned int i; | |
1618 | v4l2_std_id fixup; | |
8c85454a | 1619 | |
b9f63b25 | 1620 | if (fh->is_empress && res_locked(dev, RESOURCE_OVERLAY)) { |
9afb7377 HV |
1621 | /* Don't change the std from the mpeg device |
1622 | if overlay is active. */ | |
1623 | return -EBUSY; | |
1624 | } | |
bb881f14 | 1625 | |
8f61ae2f | 1626 | for (i = 0; i < TVNORMS; i++) |
314527ac | 1627 | if (id == tvnorms[i].id) |
8f61ae2f | 1628 | break; |
8c85454a | 1629 | |
8f61ae2f DSL |
1630 | if (i == TVNORMS) |
1631 | for (i = 0; i < TVNORMS; i++) | |
314527ac | 1632 | if (id & tvnorms[i].id) |
8f61ae2f DSL |
1633 | break; |
1634 | if (i == TVNORMS) | |
1635 | return -EINVAL; | |
8c85454a | 1636 | |
314527ac | 1637 | if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) { |
8f61ae2f DSL |
1638 | if (secam[0] == 'L' || secam[0] == 'l') { |
1639 | if (secam[1] == 'C' || secam[1] == 'c') | |
1640 | fixup = V4L2_STD_SECAM_LC; | |
1641 | else | |
1642 | fixup = V4L2_STD_SECAM_L; | |
1643 | } else { | |
1644 | if (secam[0] == 'D' || secam[0] == 'd') | |
1645 | fixup = V4L2_STD_SECAM_DK; | |
1646 | else | |
1647 | fixup = V4L2_STD_SECAM; | |
1648 | } | |
06e6588e | 1649 | for (i = 0; i < TVNORMS; i++) { |
8f61ae2f DSL |
1650 | if (fixup == tvnorms[i].id) |
1651 | break; | |
06e6588e DC |
1652 | } |
1653 | if (i == TVNORMS) | |
1654 | return -EINVAL; | |
1da177e4 | 1655 | } |
8c85454a | 1656 | |
314527ac | 1657 | id = tvnorms[i].id; |
8c85454a | 1658 | |
8f61ae2f | 1659 | mutex_lock(&dev->lock); |
b9f63b25 | 1660 | if (!fh->is_empress && res_check(fh, RESOURCE_OVERLAY)) { |
8f61ae2f | 1661 | spin_lock_irqsave(&dev->slock, flags); |
718bde1a | 1662 | stop_preview(dev); |
8f61ae2f DSL |
1663 | spin_unlock_irqrestore(&dev->slock, flags); |
1664 | ||
1665 | set_tvnorm(dev, &tvnorms[i]); | |
1666 | ||
1667 | spin_lock_irqsave(&dev->slock, flags); | |
718bde1a | 1668 | start_preview(dev); |
8f61ae2f DSL |
1669 | spin_unlock_irqrestore(&dev->slock, flags); |
1670 | } else | |
1671 | set_tvnorm(dev, &tvnorms[i]); | |
8c85454a | 1672 | |
8f61ae2f DSL |
1673 | saa7134_tvaudio_do_scan(dev); |
1674 | mutex_unlock(&dev->lock); | |
1675 | return 0; | |
1676 | } | |
b93a18d5 | 1677 | EXPORT_SYMBOL_GPL(saa7134_s_std); |
9afb7377 | 1678 | |
b93a18d5 | 1679 | int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id) |
9afb7377 | 1680 | { |
b93a18d5 | 1681 | struct saa7134_dev *dev = video_drvdata(file); |
9afb7377 HV |
1682 | |
1683 | *id = dev->tvnorm->id; | |
1684 | return 0; | |
1685 | } | |
b93a18d5 | 1686 | EXPORT_SYMBOL_GPL(saa7134_g_std); |
1da177e4 | 1687 | |
bfb12e38 | 1688 | static int saa7134_cropcap(struct file *file, void *priv, |
8f61ae2f DSL |
1689 | struct v4l2_cropcap *cap) |
1690 | { | |
b9f63b25 | 1691 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 1692 | |
8f61ae2f DSL |
1693 | if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && |
1694 | cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) | |
1695 | return -EINVAL; | |
1696 | cap->bounds = dev->crop_bounds; | |
1697 | cap->defrect = dev->crop_defrect; | |
1698 | cap->pixelaspect.numerator = 1; | |
1699 | cap->pixelaspect.denominator = 1; | |
1700 | if (dev->tvnorm->id & V4L2_STD_525_60) { | |
1701 | cap->pixelaspect.numerator = 11; | |
1702 | cap->pixelaspect.denominator = 10; | |
1703 | } | |
1704 | if (dev->tvnorm->id & V4L2_STD_625_50) { | |
1705 | cap->pixelaspect.numerator = 54; | |
1706 | cap->pixelaspect.denominator = 59; | |
1da177e4 | 1707 | } |
8f61ae2f DSL |
1708 | return 0; |
1709 | } | |
1da177e4 | 1710 | |
bfb12e38 | 1711 | static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop) |
90bdc145 | 1712 | { |
b9f63b25 | 1713 | struct saa7134_dev *dev = video_drvdata(file); |
90bdc145 MCC |
1714 | |
1715 | if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && | |
1716 | crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) | |
1717 | return -EINVAL; | |
1718 | crop->c = dev->crop_current; | |
1719 | return 0; | |
1720 | } | |
1721 | ||
4f996594 | 1722 | static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop) |
90bdc145 | 1723 | { |
b9f63b25 | 1724 | struct saa7134_dev *dev = video_drvdata(file); |
90bdc145 | 1725 | struct v4l2_rect *b = &dev->crop_bounds; |
4f996594 | 1726 | struct v4l2_rect *c = &dev->crop_current; |
90bdc145 MCC |
1727 | |
1728 | if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && | |
1729 | crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) | |
1730 | return -EINVAL; | |
1731 | if (crop->c.height < 0) | |
1732 | return -EINVAL; | |
1733 | if (crop->c.width < 0) | |
1734 | return -EINVAL; | |
1735 | ||
b9f63b25 | 1736 | if (res_locked(dev, RESOURCE_OVERLAY)) |
90bdc145 | 1737 | return -EBUSY; |
b9f63b25 | 1738 | if (res_locked(dev, RESOURCE_VIDEO)) |
90bdc145 MCC |
1739 | return -EBUSY; |
1740 | ||
4f996594 HV |
1741 | *c = crop->c; |
1742 | if (c->top < b->top) | |
1743 | c->top = b->top; | |
1744 | if (c->top > b->top + b->height) | |
1745 | c->top = b->top + b->height; | |
1746 | if (c->height > b->top - c->top + b->height) | |
1747 | c->height = b->top - c->top + b->height; | |
1748 | ||
1749 | if (c->left < b->left) | |
1750 | c->left = b->left; | |
1751 | if (c->left > b->left + b->width) | |
1752 | c->left = b->left + b->width; | |
1753 | if (c->width > b->left - c->left + b->width) | |
1754 | c->width = b->left - c->left + b->width; | |
90bdc145 MCC |
1755 | return 0; |
1756 | } | |
1757 | ||
b93a18d5 | 1758 | int saa7134_g_tuner(struct file *file, void *priv, |
8f61ae2f DSL |
1759 | struct v4l2_tuner *t) |
1760 | { | |
b93a18d5 | 1761 | struct saa7134_dev *dev = video_drvdata(file); |
8f61ae2f | 1762 | int n; |
1da177e4 | 1763 | |
8f61ae2f DSL |
1764 | if (0 != t->index) |
1765 | return -EINVAL; | |
1766 | memset(t, 0, sizeof(*t)); | |
06e6588e | 1767 | for (n = 0; n < SAA7134_INPUT_MAX; n++) { |
8f61ae2f DSL |
1768 | if (card_in(dev, n).tv) |
1769 | break; | |
06e6588e DC |
1770 | } |
1771 | if (n == SAA7134_INPUT_MAX) | |
1772 | return -EINVAL; | |
8f61ae2f DSL |
1773 | if (NULL != card_in(dev, n).name) { |
1774 | strcpy(t->name, "Television"); | |
1775 | t->type = V4L2_TUNER_ANALOG_TV; | |
ce5bdd52 | 1776 | saa_call_all(dev, tuner, g_tuner, t); |
8f61ae2f DSL |
1777 | t->capability = V4L2_TUNER_CAP_NORM | |
1778 | V4L2_TUNER_CAP_STEREO | | |
1779 | V4L2_TUNER_CAP_LANG1 | | |
1780 | V4L2_TUNER_CAP_LANG2; | |
8f61ae2f DSL |
1781 | t->rxsubchans = saa7134_tvaudio_getstereo(dev); |
1782 | t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans); | |
1783 | } | |
1784 | if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03)) | |
1785 | t->signal = 0xffff; | |
1786 | return 0; | |
1787 | } | |
b93a18d5 | 1788 | EXPORT_SYMBOL_GPL(saa7134_g_tuner); |
b4aeb8b8 | 1789 | |
b93a18d5 | 1790 | int saa7134_s_tuner(struct file *file, void *priv, |
2f73c7c5 | 1791 | const struct v4l2_tuner *t) |
8f61ae2f | 1792 | { |
b93a18d5 | 1793 | struct saa7134_dev *dev = video_drvdata(file); |
3bbaa3a6 | 1794 | int rx, mode; |
b4aeb8b8 | 1795 | |
ce5bdd52 OZ |
1796 | if (0 != t->index) |
1797 | return -EINVAL; | |
1798 | ||
8f61ae2f DSL |
1799 | mode = dev->thread.mode; |
1800 | if (UNSET == mode) { | |
1801 | rx = saa7134_tvaudio_getstereo(dev); | |
e92ba283 | 1802 | mode = saa7134_tvaudio_rx2mode(rx); |
1da177e4 | 1803 | } |
8f61ae2f DSL |
1804 | if (mode != t->audmode) |
1805 | dev->thread.mode = t->audmode; | |
8c85454a | 1806 | |
8f61ae2f DSL |
1807 | return 0; |
1808 | } | |
b93a18d5 | 1809 | EXPORT_SYMBOL_GPL(saa7134_s_tuner); |
1da177e4 | 1810 | |
b93a18d5 | 1811 | int saa7134_g_frequency(struct file *file, void *priv, |
8f61ae2f DSL |
1812 | struct v4l2_frequency *f) |
1813 | { | |
b93a18d5 | 1814 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 1815 | |
0a5ea88b OZ |
1816 | if (0 != f->tuner) |
1817 | return -EINVAL; | |
1818 | ||
0a5ea88b | 1819 | saa_call_all(dev, tuner, g_frequency, f); |
8c85454a | 1820 | |
8f61ae2f DSL |
1821 | return 0; |
1822 | } | |
b93a18d5 | 1823 | EXPORT_SYMBOL_GPL(saa7134_g_frequency); |
1da177e4 | 1824 | |
b93a18d5 | 1825 | int saa7134_s_frequency(struct file *file, void *priv, |
b530a447 | 1826 | const struct v4l2_frequency *f) |
8f61ae2f | 1827 | { |
b93a18d5 | 1828 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 1829 | |
8f61ae2f DSL |
1830 | if (0 != f->tuner) |
1831 | return -EINVAL; | |
8f61ae2f | 1832 | mutex_lock(&dev->lock); |
1da177e4 | 1833 | |
fac6986c | 1834 | saa_call_all(dev, tuner, s_frequency, f); |
1da177e4 | 1835 | |
8f61ae2f DSL |
1836 | saa7134_tvaudio_do_scan(dev); |
1837 | mutex_unlock(&dev->lock); | |
1838 | return 0; | |
1839 | } | |
b93a18d5 | 1840 | EXPORT_SYMBOL_GPL(saa7134_s_frequency); |
1da177e4 | 1841 | |
78b526a4 | 1842 | static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv, |
90bdc145 | 1843 | struct v4l2_fmtdesc *f) |
8f61ae2f | 1844 | { |
8c85454a MCC |
1845 | if (f->index >= FORMATS) |
1846 | return -EINVAL; | |
1da177e4 | 1847 | |
8c85454a MCC |
1848 | strlcpy(f->description, formats[f->index].name, |
1849 | sizeof(f->description)); | |
1da177e4 | 1850 | |
8c85454a | 1851 | f->pixelformat = formats[f->index].fourcc; |
1da177e4 | 1852 | |
8c85454a MCC |
1853 | return 0; |
1854 | } | |
1855 | ||
78b526a4 | 1856 | static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv, |
8c85454a MCC |
1857 | struct v4l2_fmtdesc *f) |
1858 | { | |
1859 | if (saa7134_no_overlay > 0) { | |
1860 | printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n"); | |
90bdc145 MCC |
1861 | return -EINVAL; |
1862 | } | |
8c85454a MCC |
1863 | |
1864 | if ((f->index >= FORMATS) || formats[f->index].planar) | |
1865 | return -EINVAL; | |
1866 | ||
1867 | strlcpy(f->description, formats[f->index].name, | |
1868 | sizeof(f->description)); | |
1869 | ||
1870 | f->pixelformat = formats[f->index].fourcc; | |
1871 | ||
1872 | return 0; | |
1873 | } | |
1874 | ||
bfb12e38 | 1875 | static int saa7134_g_fbuf(struct file *file, void *f, |
8f61ae2f DSL |
1876 | struct v4l2_framebuffer *fb) |
1877 | { | |
b9f63b25 | 1878 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 1879 | |
8f61ae2f DSL |
1880 | *fb = dev->ovbuf; |
1881 | fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING; | |
ecb73774 | 1882 | |
8f61ae2f DSL |
1883 | return 0; |
1884 | } | |
1da177e4 | 1885 | |
bfb12e38 | 1886 | static int saa7134_s_fbuf(struct file *file, void *f, |
e6eb28c2 | 1887 | const struct v4l2_framebuffer *fb) |
8f61ae2f | 1888 | { |
b9f63b25 | 1889 | struct saa7134_dev *dev = video_drvdata(file); |
8f61ae2f DSL |
1890 | struct saa7134_format *fmt; |
1891 | ||
1892 | if (!capable(CAP_SYS_ADMIN) && | |
1893 | !capable(CAP_SYS_RAWIO)) | |
1894 | return -EPERM; | |
1da177e4 | 1895 | |
8f61ae2f DSL |
1896 | /* check args */ |
1897 | fmt = format_by_fourcc(fb->fmt.pixelformat); | |
1898 | if (NULL == fmt) | |
1899 | return -EINVAL; | |
1da177e4 | 1900 | |
8f61ae2f DSL |
1901 | /* ok, accept it */ |
1902 | dev->ovbuf = *fb; | |
1903 | dev->ovfmt = fmt; | |
1904 | if (0 == dev->ovbuf.fmt.bytesperline) | |
1905 | dev->ovbuf.fmt.bytesperline = | |
1906 | dev->ovbuf.fmt.width*fmt->depth/8; | |
1907 | return 0; | |
1908 | } | |
1da177e4 | 1909 | |
b9f63b25 | 1910 | static int saa7134_overlay(struct file *file, void *priv, unsigned int on) |
8f61ae2f | 1911 | { |
b9f63b25 | 1912 | struct saa7134_dev *dev = video_drvdata(file); |
8f61ae2f DSL |
1913 | unsigned long flags; |
1914 | ||
1915 | if (on) { | |
1916 | if (saa7134_no_overlay > 0) { | |
1917 | dprintk("no_overlay\n"); | |
1918 | return -EINVAL; | |
1919 | } | |
1da177e4 | 1920 | |
b9f63b25 | 1921 | if (!res_get(dev, priv, RESOURCE_OVERLAY)) |
1da177e4 | 1922 | return -EBUSY; |
8f61ae2f | 1923 | spin_lock_irqsave(&dev->slock, flags); |
718bde1a | 1924 | start_preview(dev); |
8f61ae2f | 1925 | spin_unlock_irqrestore(&dev->slock, flags); |
1da177e4 | 1926 | } |
8f61ae2f | 1927 | if (!on) { |
b9f63b25 | 1928 | if (!res_check(priv, RESOURCE_OVERLAY)) |
8f61ae2f DSL |
1929 | return -EINVAL; |
1930 | spin_lock_irqsave(&dev->slock, flags); | |
718bde1a | 1931 | stop_preview(dev); |
8f61ae2f | 1932 | spin_unlock_irqrestore(&dev->slock, flags); |
b9f63b25 | 1933 | res_free(dev, priv, RESOURCE_OVERLAY); |
1da177e4 | 1934 | } |
8f61ae2f DSL |
1935 | return 0; |
1936 | } | |
1937 | ||
ce791139 | 1938 | int saa7134_reqbufs(struct file *file, void *priv, |
90bdc145 MCC |
1939 | struct v4l2_requestbuffers *p) |
1940 | { | |
8fcd4769 | 1941 | return videobuf_reqbufs(saa7134_queue(file), p); |
90bdc145 | 1942 | } |
ce791139 | 1943 | EXPORT_SYMBOL_GPL(saa7134_reqbufs); |
8f61ae2f | 1944 | |
ce791139 | 1945 | int saa7134_querybuf(struct file *file, void *priv, |
90bdc145 MCC |
1946 | struct v4l2_buffer *b) |
1947 | { | |
8fcd4769 | 1948 | return videobuf_querybuf(saa7134_queue(file), b); |
90bdc145 | 1949 | } |
ce791139 | 1950 | EXPORT_SYMBOL_GPL(saa7134_querybuf); |
8f61ae2f | 1951 | |
ce791139 | 1952 | int saa7134_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) |
90bdc145 | 1953 | { |
8fcd4769 | 1954 | return videobuf_qbuf(saa7134_queue(file), b); |
90bdc145 | 1955 | } |
ce791139 | 1956 | EXPORT_SYMBOL_GPL(saa7134_qbuf); |
90bdc145 | 1957 | |
ce791139 | 1958 | int saa7134_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) |
90bdc145 | 1959 | { |
8fcd4769 | 1960 | return videobuf_dqbuf(saa7134_queue(file), b, |
90bdc145 MCC |
1961 | file->f_flags & O_NONBLOCK); |
1962 | } | |
ce791139 | 1963 | EXPORT_SYMBOL_GPL(saa7134_dqbuf); |
90bdc145 | 1964 | |
ce791139 | 1965 | int saa7134_streamon(struct file *file, void *priv, |
90bdc145 MCC |
1966 | enum v4l2_buf_type type) |
1967 | { | |
b9f63b25 | 1968 | struct saa7134_dev *dev = video_drvdata(file); |
8fcd4769 | 1969 | int res = saa7134_resource(file); |
90bdc145 | 1970 | |
b9f63b25 | 1971 | if (!res_get(dev, priv, res)) |
90bdc145 MCC |
1972 | return -EBUSY; |
1973 | ||
87739868 SF |
1974 | /* The SAA7134 has a 1K FIFO; the datasheet suggests that when |
1975 | * configured conservatively, there's 22 usec of buffering for video. | |
1976 | * We therefore request a DMA latency of 20 usec, giving us 2 usec of | |
1977 | * margin in case the FIFO is configured differently to the datasheet. | |
1978 | * Unfortunately, I lack register-level documentation to check the | |
1979 | * Linux FIFO setup and confirm the perfect value. | |
1980 | */ | |
ce791139 HV |
1981 | if (res != RESOURCE_EMPRESS) |
1982 | pm_qos_add_request(&dev->qos_request, | |
1983 | PM_QOS_CPU_DMA_LATENCY, 20); | |
87739868 | 1984 | |
8fcd4769 | 1985 | return videobuf_streamon(saa7134_queue(file)); |
90bdc145 | 1986 | } |
ce791139 | 1987 | EXPORT_SYMBOL_GPL(saa7134_streamon); |
90bdc145 | 1988 | |
ce791139 | 1989 | int saa7134_streamoff(struct file *file, void *priv, |
90bdc145 MCC |
1990 | enum v4l2_buf_type type) |
1991 | { | |
b9f63b25 | 1992 | struct saa7134_dev *dev = video_drvdata(file); |
90bdc145 | 1993 | int err; |
8fcd4769 | 1994 | int res = saa7134_resource(file); |
90bdc145 | 1995 | |
ce791139 HV |
1996 | if (res != RESOURCE_EMPRESS) |
1997 | pm_qos_remove_request(&dev->qos_request); | |
87739868 | 1998 | |
8fcd4769 | 1999 | err = videobuf_streamoff(saa7134_queue(file)); |
90bdc145 MCC |
2000 | if (err < 0) |
2001 | return err; | |
b9f63b25 | 2002 | res_free(dev, priv, res); |
1da177e4 LT |
2003 | return 0; |
2004 | } | |
ce791139 | 2005 | EXPORT_SYMBOL_GPL(saa7134_streamoff); |
1da177e4 | 2006 | |
c121ba1f MCC |
2007 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
2008 | static int vidioc_g_register (struct file *file, void *priv, | |
aecde8b5 | 2009 | struct v4l2_dbg_register *reg) |
c121ba1f | 2010 | { |
b9f63b25 | 2011 | struct saa7134_dev *dev = video_drvdata(file); |
c121ba1f | 2012 | |
b8300702 | 2013 | reg->val = saa_readb(reg->reg & 0xffffff); |
aecde8b5 | 2014 | reg->size = 1; |
c121ba1f MCC |
2015 | return 0; |
2016 | } | |
2017 | ||
2018 | static int vidioc_s_register (struct file *file, void *priv, | |
977ba3b1 | 2019 | const struct v4l2_dbg_register *reg) |
c121ba1f | 2020 | { |
b9f63b25 | 2021 | struct saa7134_dev *dev = video_drvdata(file); |
c121ba1f | 2022 | |
b8300702 | 2023 | saa_writeb(reg->reg & 0xffffff, reg->val); |
c121ba1f MCC |
2024 | return 0; |
2025 | } | |
2026 | #endif | |
2027 | ||
8f61ae2f DSL |
2028 | static int radio_g_tuner(struct file *file, void *priv, |
2029 | struct v4l2_tuner *t) | |
2030 | { | |
b9f63b25 | 2031 | struct saa7134_dev *dev = video_drvdata(file); |
1da177e4 | 2032 | |
8f61ae2f DSL |
2033 | if (0 != t->index) |
2034 | return -EINVAL; | |
1da177e4 | 2035 | |
8f61ae2f | 2036 | strcpy(t->name, "Radio"); |
8f61ae2f | 2037 | |
fac6986c | 2038 | saa_call_all(dev, tuner, g_tuner, t); |
82456708 | 2039 | t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO; |
8f61ae2f DSL |
2040 | if (dev->input->amux == TV) { |
2041 | t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11); | |
2042 | t->rxsubchans = (saa_readb(0x529) & 0x08) ? | |
2043 | V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; | |
ac19ecc6 | 2044 | } |
8f61ae2f DSL |
2045 | return 0; |
2046 | } | |
2047 | static int radio_s_tuner(struct file *file, void *priv, | |
2f73c7c5 | 2048 | const struct v4l2_tuner *t) |
8f61ae2f | 2049 | { |
b9f63b25 | 2050 | struct saa7134_dev *dev = video_drvdata(file); |
ac19ecc6 | 2051 | |
8f61ae2f DSL |
2052 | if (0 != t->index) |
2053 | return -EINVAL; | |
2054 | ||
fac6986c | 2055 | saa_call_all(dev, tuner, s_tuner, t); |
8f61ae2f DSL |
2056 | return 0; |
2057 | } | |
ac19ecc6 | 2058 | |
bec43661 | 2059 | static const struct v4l2_file_operations video_fops = |
1da177e4 LT |
2060 | { |
2061 | .owner = THIS_MODULE, | |
2062 | .open = video_open, | |
2063 | .release = video_release, | |
2064 | .read = video_read, | |
2065 | .poll = video_poll, | |
2066 | .mmap = video_mmap, | |
8f61ae2f | 2067 | .ioctl = video_ioctl2, |
1da177e4 LT |
2068 | }; |
2069 | ||
a399810c | 2070 | static const struct v4l2_ioctl_ops video_ioctl_ops = { |
bfb12e38 | 2071 | .vidioc_querycap = saa7134_querycap, |
78b526a4 HV |
2072 | .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap, |
2073 | .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap, | |
2074 | .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap, | |
2075 | .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap, | |
2076 | .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay, | |
2077 | .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay, | |
2078 | .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay, | |
2079 | .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay, | |
78b526a4 HV |
2080 | .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap, |
2081 | .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap, | |
2082 | .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap, | |
bfb12e38 MCC |
2083 | .vidioc_cropcap = saa7134_cropcap, |
2084 | .vidioc_reqbufs = saa7134_reqbufs, | |
2085 | .vidioc_querybuf = saa7134_querybuf, | |
2086 | .vidioc_qbuf = saa7134_qbuf, | |
2087 | .vidioc_dqbuf = saa7134_dqbuf, | |
2088 | .vidioc_s_std = saa7134_s_std, | |
9afb7377 | 2089 | .vidioc_g_std = saa7134_g_std, |
bfb12e38 MCC |
2090 | .vidioc_enum_input = saa7134_enum_input, |
2091 | .vidioc_g_input = saa7134_g_input, | |
2092 | .vidioc_s_input = saa7134_s_input, | |
bfb12e38 MCC |
2093 | .vidioc_streamon = saa7134_streamon, |
2094 | .vidioc_streamoff = saa7134_streamoff, | |
2095 | .vidioc_g_tuner = saa7134_g_tuner, | |
2096 | .vidioc_s_tuner = saa7134_s_tuner, | |
bfb12e38 MCC |
2097 | .vidioc_g_crop = saa7134_g_crop, |
2098 | .vidioc_s_crop = saa7134_s_crop, | |
2099 | .vidioc_g_fbuf = saa7134_g_fbuf, | |
2100 | .vidioc_s_fbuf = saa7134_s_fbuf, | |
2101 | .vidioc_overlay = saa7134_overlay, | |
bfb12e38 MCC |
2102 | .vidioc_g_frequency = saa7134_g_frequency, |
2103 | .vidioc_s_frequency = saa7134_s_frequency, | |
c121ba1f MCC |
2104 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
2105 | .vidioc_g_register = vidioc_g_register, | |
2106 | .vidioc_s_register = vidioc_s_register, | |
2107 | #endif | |
a2004502 HV |
2108 | .vidioc_log_status = v4l2_ctrl_log_status, |
2109 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
2110 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
1da177e4 LT |
2111 | }; |
2112 | ||
bec43661 | 2113 | static const struct v4l2_file_operations radio_fops = { |
a399810c HV |
2114 | .owner = THIS_MODULE, |
2115 | .open = video_open, | |
2983baf8 | 2116 | .read = radio_read, |
a399810c HV |
2117 | .release = video_release, |
2118 | .ioctl = video_ioctl2, | |
2983baf8 | 2119 | .poll = radio_poll, |
a399810c HV |
2120 | }; |
2121 | ||
2122 | static const struct v4l2_ioctl_ops radio_ioctl_ops = { | |
c3b3e0c5 | 2123 | .vidioc_querycap = saa7134_querycap, |
8f61ae2f | 2124 | .vidioc_g_tuner = radio_g_tuner, |
8f61ae2f | 2125 | .vidioc_s_tuner = radio_s_tuner, |
bfb12e38 MCC |
2126 | .vidioc_g_frequency = saa7134_g_frequency, |
2127 | .vidioc_s_frequency = saa7134_s_frequency, | |
a2004502 HV |
2128 | .vidioc_log_status = v4l2_ctrl_log_status, |
2129 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
2130 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
1da177e4 LT |
2131 | }; |
2132 | ||
a399810c HV |
2133 | /* ----------------------------------------------------------- */ |
2134 | /* exported stuff */ | |
2135 | ||
2136 | struct video_device saa7134_video_template = { | |
2137 | .name = "saa7134-video", | |
a399810c HV |
2138 | .fops = &video_fops, |
2139 | .ioctl_ops = &video_ioctl_ops, | |
a399810c | 2140 | .tvnorms = SAA7134_NORMS, |
a399810c HV |
2141 | }; |
2142 | ||
2143 | struct video_device saa7134_radio_template = { | |
2144 | .name = "saa7134-radio", | |
a399810c HV |
2145 | .fops = &radio_fops, |
2146 | .ioctl_ops = &radio_ioctl_ops, | |
a399810c HV |
2147 | }; |
2148 | ||
718bde1a HV |
2149 | static const struct v4l2_ctrl_ops saa7134_ctrl_ops = { |
2150 | .s_ctrl = saa7134_s_ctrl, | |
2151 | }; | |
2152 | ||
2153 | static const struct v4l2_ctrl_config saa7134_ctrl_invert = { | |
2154 | .ops = &saa7134_ctrl_ops, | |
2155 | .id = V4L2_CID_PRIVATE_INVERT, | |
2156 | .name = "Invert", | |
2157 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
2158 | .min = 0, | |
2159 | .max = 1, | |
2160 | .step = 1, | |
2161 | }; | |
2162 | ||
2163 | static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = { | |
2164 | .ops = &saa7134_ctrl_ops, | |
2165 | .id = V4L2_CID_PRIVATE_Y_ODD, | |
2166 | .name = "Y Offset Odd Field", | |
2167 | .type = V4L2_CTRL_TYPE_INTEGER, | |
2168 | .min = 0, | |
2169 | .max = 128, | |
2170 | .step = 1, | |
2171 | }; | |
2172 | ||
2173 | static const struct v4l2_ctrl_config saa7134_ctrl_y_even = { | |
2174 | .ops = &saa7134_ctrl_ops, | |
2175 | .id = V4L2_CID_PRIVATE_Y_EVEN, | |
2176 | .name = "Y Offset Even Field", | |
2177 | .type = V4L2_CTRL_TYPE_INTEGER, | |
2178 | .min = 0, | |
2179 | .max = 128, | |
2180 | .step = 1, | |
2181 | }; | |
2182 | ||
2183 | static const struct v4l2_ctrl_config saa7134_ctrl_automute = { | |
2184 | .ops = &saa7134_ctrl_ops, | |
2185 | .id = V4L2_CID_PRIVATE_AUTOMUTE, | |
2186 | .name = "Automute", | |
2187 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
2188 | .min = 0, | |
2189 | .max = 1, | |
2190 | .step = 1, | |
2191 | .def = 1, | |
2192 | }; | |
2193 | ||
1da177e4 LT |
2194 | int saa7134_video_init1(struct saa7134_dev *dev) |
2195 | { | |
718bde1a HV |
2196 | struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler; |
2197 | ||
1da177e4 LT |
2198 | /* sanitycheck insmod options */ |
2199 | if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME) | |
2200 | gbuffers = 2; | |
3eeba4a7 | 2201 | if (gbufsize > gbufsize_max) |
1da177e4 LT |
2202 | gbufsize = gbufsize_max; |
2203 | gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK; | |
2204 | ||
718bde1a HV |
2205 | v4l2_ctrl_handler_init(hdl, 11); |
2206 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2207 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); | |
2208 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2209 | V4L2_CID_CONTRAST, 0, 127, 1, 68); | |
2210 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2211 | V4L2_CID_SATURATION, 0, 127, 1, 64); | |
2212 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2213 | V4L2_CID_HUE, -128, 127, 1, 0); | |
2214 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2215 | V4L2_CID_HFLIP, 0, 1, 1, 0); | |
2216 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2217 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); | |
2218 | v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops, | |
2219 | V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0); | |
2220 | v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL); | |
2221 | v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL); | |
2222 | v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL); | |
2223 | v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL); | |
2224 | if (hdl->error) | |
2225 | return hdl->error; | |
2226 | if (card_has_radio(dev)) { | |
2227 | hdl = &dev->radio_ctrl_handler; | |
2228 | v4l2_ctrl_handler_init(hdl, 2); | |
2229 | v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, | |
2230 | v4l2_ctrl_radio_filter); | |
2231 | if (hdl->error) | |
2232 | return hdl->error; | |
2233 | } | |
2234 | dev->ctl_mute = 1; | |
2235 | ||
2236 | if (dev->tda9887_conf && saa7134_ctrl_automute.def) | |
1da177e4 LT |
2237 | dev->tda9887_conf |= TDA9887_AUTOMUTE; |
2238 | dev->automute = 0; | |
2239 | ||
4ac97914 | 2240 | INIT_LIST_HEAD(&dev->video_q.queue); |
1da177e4 LT |
2241 | init_timer(&dev->video_q.timeout); |
2242 | dev->video_q.timeout.function = saa7134_buffer_timeout; | |
2243 | dev->video_q.timeout.data = (unsigned long)(&dev->video_q); | |
2244 | dev->video_q.dev = dev; | |
813b9dff HV |
2245 | dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24); |
2246 | dev->width = 720; | |
2247 | dev->height = 576; | |
3a0a5a78 HV |
2248 | dev->win.w.width = dev->width; |
2249 | dev->win.w.height = dev->height; | |
2250 | dev->win.field = V4L2_FIELD_INTERLACED; | |
2251 | dev->ovbuf.fmt.width = dev->width; | |
2252 | dev->ovbuf.fmt.height = dev->height; | |
2253 | dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc; | |
2254 | dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1da177e4 | 2255 | |
cb71201f ML |
2256 | if (saa7134_boards[dev->board].video_out) |
2257 | saa7134_videoport_init(dev); | |
2258 | ||
9db0fb18 HV |
2259 | videobuf_queue_sg_init(&dev->cap, &video_qops, |
2260 | &dev->pci->dev, &dev->slock, | |
2261 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
2262 | V4L2_FIELD_INTERLACED, | |
2263 | sizeof(struct saa7134_buf), | |
2264 | dev, NULL); | |
2265 | videobuf_queue_sg_init(&dev->vbi, &saa7134_vbi_qops, | |
2266 | &dev->pci->dev, &dev->slock, | |
2267 | V4L2_BUF_TYPE_VBI_CAPTURE, | |
2268 | V4L2_FIELD_SEQ_TB, | |
2269 | sizeof(struct saa7134_buf), | |
2270 | dev, NULL); | |
2271 | saa7134_pgtable_alloc(dev->pci, &dev->pt_cap); | |
2272 | saa7134_pgtable_alloc(dev->pci, &dev->pt_vbi); | |
2273 | ||
cb71201f ML |
2274 | return 0; |
2275 | } | |
2276 | ||
9db0fb18 HV |
2277 | void saa7134_video_fini(struct saa7134_dev *dev) |
2278 | { | |
2279 | /* free stuff */ | |
2280 | saa7134_pgtable_free(dev->pci, &dev->pt_cap); | |
2281 | saa7134_pgtable_free(dev->pci, &dev->pt_vbi); | |
718bde1a HV |
2282 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
2283 | if (card_has_radio(dev)) | |
2284 | v4l2_ctrl_handler_free(&dev->radio_ctrl_handler); | |
9db0fb18 HV |
2285 | } |
2286 | ||
cb71201f ML |
2287 | int saa7134_videoport_init(struct saa7134_dev *dev) |
2288 | { | |
2289 | /* enable video output */ | |
2290 | int vo = saa7134_boards[dev->board].video_out; | |
2291 | int video_reg; | |
2292 | unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts; | |
37df96d3 DB |
2293 | |
2294 | /* Configure videoport */ | |
cb71201f ML |
2295 | saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]); |
2296 | video_reg = video_out[vo][1]; | |
2297 | if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED) | |
2298 | video_reg &= ~VP_T_CODE_P_INVERTED; | |
2299 | saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg); | |
2300 | saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]); | |
cb71201f ML |
2301 | saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]); |
2302 | video_reg = video_out[vo][5]; | |
2303 | if (vid_port_opts & SET_CLOCK_NOT_DELAYED) | |
2304 | video_reg &= ~VP_CLK_CTRL2_DELAYED; | |
2305 | if (vid_port_opts & SET_CLOCK_INVERTED) | |
2306 | video_reg |= VP_CLK_CTRL1_INVERTED; | |
2307 | saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg); | |
2308 | video_reg = video_out[vo][6]; | |
2309 | if (vid_port_opts & SET_VSYNC_OFF) { | |
2310 | video_reg &= ~VP_VS_TYPE_MASK; | |
2311 | video_reg |= VP_VS_TYPE_OFF; | |
2312 | } | |
2313 | saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg); | |
2314 | saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]); | |
2315 | saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]); | |
1da177e4 | 2316 | |
37df96d3 DB |
2317 | /* Start videoport */ |
2318 | saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]); | |
2319 | ||
1da177e4 LT |
2320 | return 0; |
2321 | } | |
2322 | ||
2323 | int saa7134_video_init2(struct saa7134_dev *dev) | |
2324 | { | |
2325 | /* init video hw */ | |
2326 | set_tvnorm(dev,&tvnorms[0]); | |
2327 | video_mux(dev,0); | |
718bde1a | 2328 | v4l2_ctrl_handler_setup(&dev->ctrl_handler); |
1da177e4 LT |
2329 | saa7134_tvaudio_setmute(dev); |
2330 | saa7134_tvaudio_setvolume(dev,dev->ctl_volume); | |
2331 | return 0; | |
2332 | } | |
2333 | ||
11f7078c | 2334 | void saa7134_irq_video_signalchange(struct saa7134_dev *dev) |
1da177e4 LT |
2335 | { |
2336 | static const char *st[] = { | |
2337 | "(no signal)", "NTSC", "PAL", "SECAM" }; | |
2338 | u32 st1,st2; | |
2339 | ||
2340 | st1 = saa_readb(SAA7134_STATUS_VIDEO1); | |
2341 | st2 = saa_readb(SAA7134_STATUS_VIDEO2); | |
2342 | dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n", | |
2343 | (st1 & 0x40) ? "not locked" : "locked", | |
2344 | (st2 & 0x40) ? "no" : "yes", | |
2345 | st[st1 & 0x03]); | |
11f7078c | 2346 | dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1); |
1da177e4 LT |
2347 | |
2348 | if (dev->nosignal) { | |
2349 | /* no video signal -> mute audio */ | |
2350 | if (dev->ctl_automute) | |
2351 | dev->automute = 1; | |
2352 | saa7134_tvaudio_setmute(dev); | |
1da177e4 LT |
2353 | } else { |
2354 | /* wake up tvaudio audio carrier scan thread */ | |
2355 | saa7134_tvaudio_do_scan(dev); | |
1da177e4 | 2356 | } |
11f7078c ML |
2357 | |
2358 | if ((st2 & 0x80) && !noninterlaced && !dev->nosignal) | |
2359 | saa_clearb(SAA7134_SYNC_CTRL, 0x20); | |
2360 | else | |
2361 | saa_setb(SAA7134_SYNC_CTRL, 0x20); | |
2362 | ||
1da177e4 LT |
2363 | if (dev->mops && dev->mops->signal_change) |
2364 | dev->mops->signal_change(dev); | |
2365 | } | |
2366 | ||
11f7078c | 2367 | |
1da177e4 LT |
2368 | void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status) |
2369 | { | |
2370 | enum v4l2_field field; | |
2371 | ||
2372 | spin_lock(&dev->slock); | |
2373 | if (dev->video_q.curr) { | |
2374 | dev->video_fieldcount++; | |
2375 | field = dev->video_q.curr->vb.field; | |
2376 | if (V4L2_FIELD_HAS_BOTH(field)) { | |
2377 | /* make sure we have seen both fields */ | |
2378 | if ((status & 0x10) == 0x00) { | |
2379 | dev->video_q.curr->top_seen = 1; | |
2380 | goto done; | |
2381 | } | |
2382 | if (!dev->video_q.curr->top_seen) | |
2383 | goto done; | |
2384 | } else if (field == V4L2_FIELD_TOP) { | |
2385 | if ((status & 0x10) != 0x10) | |
2386 | goto done; | |
2387 | } else if (field == V4L2_FIELD_BOTTOM) { | |
2388 | if ((status & 0x10) != 0x00) | |
2389 | goto done; | |
2390 | } | |
2391 | dev->video_q.curr->vb.field_count = dev->video_fieldcount; | |
0fc0686e | 2392 | saa7134_buffer_finish(dev,&dev->video_q,VIDEOBUF_DONE); |
1da177e4 LT |
2393 | } |
2394 | saa7134_buffer_next(dev,&dev->video_q); | |
2395 | ||
2396 | done: | |
2397 | spin_unlock(&dev->slock); | |
2398 | } | |
2399 | ||
2400 | /* ----------------------------------------------------------- */ | |
2401 | /* | |
2402 | * Local variables: | |
2403 | * c-basic-offset: 8 | |
2404 | * End: | |
2405 | */ |