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