2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
27 * bttv - Bt848 frame grabber driver
28 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
29 * & Marcus Metzler (mocm@thp.uni-koeln.de)
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47 #include <linux/version.h>
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/pci.h>
53 #include <linux/vmalloc.h>
54 #include <linux/wait.h>
56 #include <linux/interrupt.h>
57 #include <linux/i2c.h>
58 #include <linux/i2c-algo-bit.h>
60 #include <linux/spinlock.h>
61 #define MAP_NR(x) virt_to_page(x)
62 #define ZORAN_VID_TYPE ( \
68 VID_TYPE_MJPEG_DECODER | \
69 VID_TYPE_MJPEG_ENCODER \
72 #include <linux/videodev.h>
73 #include <media/v4l2-common.h>
74 #include <media/v4l2-ioctl.h>
75 #include "videocodec.h"
77 #include <asm/byteorder.h>
79 #include <asm/uaccess.h>
80 #include <linux/proc_fs.h>
82 #include <linux/video_decoder.h>
83 #include <linux/video_encoder.h>
84 #include <linux/mutex.h>
86 #include "zoran_device.h"
87 #include "zoran_card.h"
89 /* we declare some card type definitions here, they mean
90 * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
91 #define ZORAN_V4L2_VID_FLAGS ( \
93 V4L2_CAP_VIDEO_CAPTURE |\
94 V4L2_CAP_VIDEO_OUTPUT |\
95 V4L2_CAP_VIDEO_OVERLAY \
99 #if defined(CONFIG_VIDEO_V4L1_COMPAT)
100 #define ZFMT(pal, fcc, cs) \
101 .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
103 #define ZFMT(pal, fcc, cs) \
104 .fourcc = (fcc), .colorspace = (cs)
107 const struct zoran_format zoran_formats
[] = {
109 .name
= "15-bit RGB LE",
110 ZFMT(VIDEO_PALETTE_RGB555
,
111 V4L2_PIX_FMT_RGB555
, V4L2_COLORSPACE_SRGB
),
113 .flags
= ZORAN_FORMAT_CAPTURE
|
114 ZORAN_FORMAT_OVERLAY
,
115 .vfespfr
= ZR36057_VFESPFR_RGB555
|ZR36057_VFESPFR_ErrDif
|
116 ZR36057_VFESPFR_LittleEndian
,
118 .name
= "15-bit RGB BE",
120 V4L2_PIX_FMT_RGB555X
, V4L2_COLORSPACE_SRGB
),
122 .flags
= ZORAN_FORMAT_CAPTURE
|
123 ZORAN_FORMAT_OVERLAY
,
124 .vfespfr
= ZR36057_VFESPFR_RGB555
|ZR36057_VFESPFR_ErrDif
,
126 .name
= "16-bit RGB LE",
127 ZFMT(VIDEO_PALETTE_RGB565
,
128 V4L2_PIX_FMT_RGB565
, V4L2_COLORSPACE_SRGB
),
130 .flags
= ZORAN_FORMAT_CAPTURE
|
131 ZORAN_FORMAT_OVERLAY
,
132 .vfespfr
= ZR36057_VFESPFR_RGB565
|ZR36057_VFESPFR_ErrDif
|
133 ZR36057_VFESPFR_LittleEndian
,
135 .name
= "16-bit RGB BE",
137 V4L2_PIX_FMT_RGB565X
, V4L2_COLORSPACE_SRGB
),
139 .flags
= ZORAN_FORMAT_CAPTURE
|
140 ZORAN_FORMAT_OVERLAY
,
141 .vfespfr
= ZR36057_VFESPFR_RGB565
|ZR36057_VFESPFR_ErrDif
,
143 .name
= "24-bit RGB",
144 ZFMT(VIDEO_PALETTE_RGB24
,
145 V4L2_PIX_FMT_BGR24
, V4L2_COLORSPACE_SRGB
),
147 .flags
= ZORAN_FORMAT_CAPTURE
|
148 ZORAN_FORMAT_OVERLAY
,
149 .vfespfr
= ZR36057_VFESPFR_RGB888
|ZR36057_VFESPFR_Pack24
,
151 .name
= "32-bit RGB LE",
152 ZFMT(VIDEO_PALETTE_RGB32
,
153 V4L2_PIX_FMT_BGR32
, V4L2_COLORSPACE_SRGB
),
155 .flags
= ZORAN_FORMAT_CAPTURE
|
156 ZORAN_FORMAT_OVERLAY
,
157 .vfespfr
= ZR36057_VFESPFR_RGB888
|ZR36057_VFESPFR_LittleEndian
,
159 .name
= "32-bit RGB BE",
161 V4L2_PIX_FMT_RGB32
, V4L2_COLORSPACE_SRGB
),
163 .flags
= ZORAN_FORMAT_CAPTURE
|
164 ZORAN_FORMAT_OVERLAY
,
165 .vfespfr
= ZR36057_VFESPFR_RGB888
,
167 .name
= "4:2:2, packed, YUYV",
168 ZFMT(VIDEO_PALETTE_YUV422
,
169 V4L2_PIX_FMT_YUYV
, V4L2_COLORSPACE_SMPTE170M
),
171 .flags
= ZORAN_FORMAT_CAPTURE
|
172 ZORAN_FORMAT_OVERLAY
,
173 .vfespfr
= ZR36057_VFESPFR_YUV422
,
175 .name
= "4:2:2, packed, UYVY",
176 ZFMT(VIDEO_PALETTE_UYVY
,
177 V4L2_PIX_FMT_UYVY
, V4L2_COLORSPACE_SMPTE170M
),
179 .flags
= ZORAN_FORMAT_CAPTURE
|
180 ZORAN_FORMAT_OVERLAY
,
181 .vfespfr
= ZR36057_VFESPFR_YUV422
|ZR36057_VFESPFR_LittleEndian
,
183 .name
= "Hardware-encoded Motion-JPEG",
185 V4L2_PIX_FMT_MJPEG
, V4L2_COLORSPACE_SMPTE170M
),
187 .flags
= ZORAN_FORMAT_CAPTURE
|
188 ZORAN_FORMAT_PLAYBACK
|
189 ZORAN_FORMAT_COMPRESSED
,
192 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
194 static int lock_norm
; /* 0 = default 1 = Don't change TV standard (norm) */
195 module_param(lock_norm
, int, 0644);
196 MODULE_PARM_DESC(lock_norm
, "Prevent norm changes (1 = ignore, >1 = fail)");
198 /* small helper function for calculating buffersizes for v4l2
199 * we calculate the nearest higher power-of-two, which
200 * will be the recommended buffersize */
202 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings
*settings
)
204 __u8 div
= settings
->VerDcm
* settings
->HorDcm
* settings
->TmpDcm
;
205 __u32 num
= (1024 * 512) / (div
);
214 if (result
> jpg_bufsize
)
221 /* forward references */
222 static void v4l_fbuffer_free(struct file
*file
);
223 static void jpg_fbuffer_free(struct file
*file
);
226 * Allocate the V4L grab buffers
228 * These have to be pysically contiguous.
232 v4l_fbuffer_alloc (struct file
*file
)
234 struct zoran_fh
*fh
= file
->private_data
;
235 struct zoran
*zr
= fh
->zr
;
239 for (i
= 0; i
< fh
->v4l_buffers
.num_buffers
; i
++) {
240 if (fh
->v4l_buffers
.buffer
[i
].fbuffer
)
243 "%s: v4l_fbuffer_alloc() - buffer %d already allocated!?\n",
247 mem
= kmalloc(fh
->v4l_buffers
.buffer_size
, GFP_KERNEL
);
251 "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
253 v4l_fbuffer_free(file
);
256 fh
->v4l_buffers
.buffer
[i
].fbuffer
= mem
;
257 fh
->v4l_buffers
.buffer
[i
].fbuffer_phys
=
259 fh
->v4l_buffers
.buffer
[i
].fbuffer_bus
=
261 for (off
= 0; off
< fh
->v4l_buffers
.buffer_size
;
263 SetPageReserved(MAP_NR(mem
+ off
));
266 "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
267 ZR_DEVNAME(zr
), i
, (unsigned long) mem
,
271 fh
->v4l_buffers
.allocated
= 1;
276 /* free the V4L grab buffers */
278 v4l_fbuffer_free (struct file
*file
)
280 struct zoran_fh
*fh
= file
->private_data
;
281 struct zoran
*zr
= fh
->zr
;
285 dprintk(4, KERN_INFO
"%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr
));
287 for (i
= 0; i
< fh
->v4l_buffers
.num_buffers
; i
++) {
288 if (!fh
->v4l_buffers
.buffer
[i
].fbuffer
)
291 mem
= fh
->v4l_buffers
.buffer
[i
].fbuffer
;
292 for (off
= 0; off
< fh
->v4l_buffers
.buffer_size
;
294 ClearPageReserved(MAP_NR(mem
+ off
));
295 kfree((void *) fh
->v4l_buffers
.buffer
[i
].fbuffer
);
296 fh
->v4l_buffers
.buffer
[i
].fbuffer
= NULL
;
299 fh
->v4l_buffers
.allocated
= 0;
303 * Allocate the MJPEG grab buffers.
305 * If a Natoma chipset is present and this is a revision 1 zr36057,
306 * each MJPEG buffer needs to be physically contiguous.
307 * (RJ: This statement is from Dave Perks' original driver,
308 * I could never check it because I have a zr36067)
310 * RJ: The contents grab buffers needs never be accessed in the driver.
311 * Therefore there is no need to allocate them with vmalloc in order
312 * to get a contiguous virtual memory space.
313 * I don't understand why many other drivers first allocate them with
314 * vmalloc (which uses internally also get_zeroed_page, but delivers you
315 * virtual addresses) and then again have to make a lot of efforts
316 * to get the physical address.
319 * On big-endian architectures (such as ppc) some extra steps
320 * are needed. When reading and writing to the stat_com array
321 * and fragment buffers, the device expects to see little-
322 * endian values. The use of cpu_to_le32() and le32_to_cpu()
323 * in this function (and one or two others in zoran_device.c)
324 * ensure that these values are always stored in little-endian
325 * form, regardless of architecture. The zr36057 does Very Bad
326 * Things on big endian architectures if the stat_com array
327 * and fragment buffers are not little-endian.
331 jpg_fbuffer_alloc (struct file
*file
)
333 struct zoran_fh
*fh
= file
->private_data
;
334 struct zoran
*zr
= fh
->zr
;
338 for (i
= 0; i
< fh
->jpg_buffers
.num_buffers
; i
++) {
339 if (fh
->jpg_buffers
.buffer
[i
].frag_tab
)
342 "%s: jpg_fbuffer_alloc() - buffer %d already allocated!?\n",
345 /* Allocate fragment table for this buffer */
347 mem
= get_zeroed_page(GFP_KERNEL
);
351 "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
353 jpg_fbuffer_free(file
);
356 fh
->jpg_buffers
.buffer
[i
].frag_tab
= (__le32
*) mem
;
357 fh
->jpg_buffers
.buffer
[i
].frag_tab_bus
=
358 virt_to_bus((void *) mem
);
360 //if (alloc_contig) {
361 if (fh
->jpg_buffers
.need_contiguous
) {
363 (unsigned long) kmalloc(fh
->jpg_buffers
.
369 "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
371 jpg_fbuffer_free(file
);
374 fh
->jpg_buffers
.buffer
[i
].frag_tab
[0] =
375 cpu_to_le32(virt_to_bus((void *) mem
));
376 fh
->jpg_buffers
.buffer
[i
].frag_tab
[1] =
377 cpu_to_le32(((fh
->jpg_buffers
.buffer_size
/ 4) << 1) | 1);
378 for (off
= 0; off
< fh
->jpg_buffers
.buffer_size
;
380 SetPageReserved(MAP_NR(mem
+ off
));
382 /* jpg_bufsize is already page aligned */
384 j
< fh
->jpg_buffers
.buffer_size
/ PAGE_SIZE
;
386 mem
= get_zeroed_page(GFP_KERNEL
);
390 "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
392 jpg_fbuffer_free(file
);
396 fh
->jpg_buffers
.buffer
[i
].frag_tab
[2 * j
] =
397 cpu_to_le32(virt_to_bus((void *) mem
));
398 fh
->jpg_buffers
.buffer
[i
].frag_tab
[2 * j
+
400 cpu_to_le32((PAGE_SIZE
/ 4) << 1);
401 SetPageReserved(MAP_NR(mem
));
404 fh
->jpg_buffers
.buffer
[i
].frag_tab
[2 * j
- 1] |= cpu_to_le32(1);
409 KERN_DEBUG
"%s: jpg_fbuffer_alloc() - %d KB allocated\n",
411 (fh
->jpg_buffers
.num_buffers
*
412 fh
->jpg_buffers
.buffer_size
) >> 10);
414 fh
->jpg_buffers
.allocated
= 1;
419 /* free the MJPEG grab buffers */
421 jpg_fbuffer_free (struct file
*file
)
423 struct zoran_fh
*fh
= file
->private_data
;
424 struct zoran
*zr
= fh
->zr
;
428 dprintk(4, KERN_DEBUG
"%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr
));
430 for (i
= 0; i
< fh
->jpg_buffers
.num_buffers
; i
++) {
431 if (!fh
->jpg_buffers
.buffer
[i
].frag_tab
)
434 //if (alloc_contig) {
435 if (fh
->jpg_buffers
.need_contiguous
) {
436 if (fh
->jpg_buffers
.buffer
[i
].frag_tab
[0]) {
437 mem
= (unsigned char *) bus_to_virt(le32_to_cpu(
438 fh
->jpg_buffers
.buffer
[i
].frag_tab
[0]));
440 off
< fh
->jpg_buffers
.buffer_size
;
442 ClearPageReserved(MAP_NR
445 fh
->jpg_buffers
.buffer
[i
].frag_tab
[0] = 0;
446 fh
->jpg_buffers
.buffer
[i
].frag_tab
[1] = 0;
450 j
< fh
->jpg_buffers
.buffer_size
/ PAGE_SIZE
;
452 if (!fh
->jpg_buffers
.buffer
[i
].
455 ClearPageReserved(MAP_NR
459 buffer
[i
].frag_tab
[2 *
461 free_page((unsigned long)
467 fh
->jpg_buffers
.buffer
[i
].frag_tab
[2 * j
] =
469 fh
->jpg_buffers
.buffer
[i
].frag_tab
[2 * j
+
474 free_page((unsigned long) fh
->jpg_buffers
.buffer
[i
].
476 fh
->jpg_buffers
.buffer
[i
].frag_tab
= NULL
;
479 fh
->jpg_buffers
.allocated
= 0;
483 * V4L Buffer grabbing
487 zoran_v4l_set_format (struct file
*file
,
490 const struct zoran_format
*format
)
492 struct zoran_fh
*fh
= file
->private_data
;
493 struct zoran
*zr
= fh
->zr
;
496 /* Check size and format of the grab wanted */
498 if (height
< BUZ_MIN_HEIGHT
|| width
< BUZ_MIN_WIDTH
||
499 height
> BUZ_MAX_HEIGHT
|| width
> BUZ_MAX_WIDTH
) {
502 "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
503 ZR_DEVNAME(zr
), width
, height
);
507 bpp
= (format
->depth
+ 7) / 8;
509 /* Check against available buffer size */
510 if (height
* width
* bpp
> fh
->v4l_buffers
.buffer_size
) {
513 "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
514 ZR_DEVNAME(zr
), fh
->v4l_buffers
.buffer_size
>> 10);
518 /* The video front end needs 4-byte alinged line sizes */
520 if ((bpp
== 2 && (width
& 1)) || (bpp
== 3 && (width
& 3))) {
523 "%s: v4l_set_format() - wrong frame alignment\n",
528 fh
->v4l_settings
.width
= width
;
529 fh
->v4l_settings
.height
= height
;
530 fh
->v4l_settings
.format
= format
;
531 fh
->v4l_settings
.bytesperline
= bpp
* fh
->v4l_settings
.width
;
537 zoran_v4l_queue_frame (struct file
*file
,
540 struct zoran_fh
*fh
= file
->private_data
;
541 struct zoran
*zr
= fh
->zr
;
545 if (!fh
->v4l_buffers
.allocated
) {
548 "%s: v4l_queue_frame() - buffers not yet allocated\n",
553 /* No grabbing outside the buffer range! */
554 if (num
>= fh
->v4l_buffers
.num_buffers
|| num
< 0) {
557 "%s: v4l_queue_frame() - buffer %d is out of range\n",
558 ZR_DEVNAME(zr
), num
);
562 spin_lock_irqsave(&zr
->spinlock
, flags
);
564 if (fh
->v4l_buffers
.active
== ZORAN_FREE
) {
565 if (zr
->v4l_buffers
.active
== ZORAN_FREE
) {
566 zr
->v4l_buffers
= fh
->v4l_buffers
;
567 fh
->v4l_buffers
.active
= ZORAN_ACTIVE
;
571 "%s: v4l_queue_frame() - another session is already capturing\n",
577 /* make sure a grab isn't going on currently with this buffer */
579 switch (zr
->v4l_buffers
.buffer
[num
].state
) {
582 if (zr
->v4l_buffers
.active
== ZORAN_FREE
) {
583 fh
->v4l_buffers
.active
= ZORAN_FREE
;
584 zr
->v4l_buffers
.allocated
= 0;
586 res
= -EBUSY
; /* what are you doing? */
591 "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
592 ZR_DEVNAME(zr
), num
);
594 /* since there is at least one unused buffer there's room for at least
595 * one more pend[] entry */
596 zr
->v4l_pend
[zr
->v4l_pend_head
++ &
597 V4L_MASK_FRAME
] = num
;
598 zr
->v4l_buffers
.buffer
[num
].state
= BUZ_STATE_PEND
;
599 zr
->v4l_buffers
.buffer
[num
].bs
.length
=
600 fh
->v4l_settings
.bytesperline
*
601 zr
->v4l_settings
.height
;
602 fh
->v4l_buffers
.buffer
[num
] =
603 zr
->v4l_buffers
.buffer
[num
];
608 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
610 if (!res
&& zr
->v4l_buffers
.active
== ZORAN_FREE
)
611 zr
->v4l_buffers
.active
= fh
->v4l_buffers
.active
;
617 v4l_grab (struct file
*file
,
618 struct video_mmap
*mp
)
620 struct zoran_fh
*fh
= file
->private_data
;
621 struct zoran
*zr
= fh
->zr
;
624 for (i
= 0; i
< NUM_FORMATS
; i
++) {
625 if (zoran_formats
[i
].palette
== mp
->format
&&
626 zoran_formats
[i
].flags
& ZORAN_FORMAT_CAPTURE
&&
627 !(zoran_formats
[i
].flags
& ZORAN_FORMAT_COMPRESSED
))
630 if (i
== NUM_FORMATS
|| zoran_formats
[i
].depth
== 0) {
633 "%s: v4l_grab() - wrong bytes-per-pixel format\n",
639 * To minimize the time spent in the IRQ routine, we avoid setting up
640 * the video front end there.
641 * If this grab has different parameters from a running streaming capture
642 * we stop the streaming capture and start it over again.
644 if (zr
->v4l_memgrab_active
&&
645 (zr
->v4l_settings
.width
!= mp
->width
||
646 zr
->v4l_settings
.height
!= mp
->height
||
647 zr
->v4l_settings
.format
->palette
!= mp
->format
)) {
648 res
= wait_grab_pending(zr
);
652 if ((res
= zoran_v4l_set_format(file
,
657 zr
->v4l_settings
= fh
->v4l_settings
;
659 /* queue the frame in the pending queue */
660 if ((res
= zoran_v4l_queue_frame(file
, mp
->frame
))) {
661 fh
->v4l_buffers
.active
= ZORAN_FREE
;
665 /* put the 36057 into frame grabbing mode */
666 if (!res
&& !zr
->v4l_memgrab_active
)
667 zr36057_set_memgrab(zr
, 1);
669 //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
675 * Sync on a V4L buffer
679 v4l_sync (struct file
*file
,
682 struct zoran_fh
*fh
= file
->private_data
;
683 struct zoran
*zr
= fh
->zr
;
686 if (fh
->v4l_buffers
.active
== ZORAN_FREE
) {
689 "%s: v4l_sync() - no grab active for this session\n",
694 /* check passed-in frame number */
695 if (frame
>= fh
->v4l_buffers
.num_buffers
|| frame
< 0) {
697 KERN_ERR
"%s: v4l_sync() - frame %d is invalid\n",
698 ZR_DEVNAME(zr
), frame
);
702 /* Check if is buffer was queued at all */
703 if (zr
->v4l_buffers
.buffer
[frame
].state
== BUZ_STATE_USER
) {
706 "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
711 /* wait on this buffer to get ready */
712 if (!wait_event_interruptible_timeout(zr
->v4l_capq
,
713 (zr
->v4l_buffers
.buffer
[frame
].state
!= BUZ_STATE_PEND
),
716 if (signal_pending(current
))
719 /* buffer should now be in BUZ_STATE_DONE */
720 if (zr
->v4l_buffers
.buffer
[frame
].state
!= BUZ_STATE_DONE
)
722 KERN_ERR
"%s: v4l_sync() - internal state error\n",
725 zr
->v4l_buffers
.buffer
[frame
].state
= BUZ_STATE_USER
;
726 fh
->v4l_buffers
.buffer
[frame
] = zr
->v4l_buffers
.buffer
[frame
];
728 spin_lock_irqsave(&zr
->spinlock
, flags
);
730 /* Check if streaming capture has finished */
731 if (zr
->v4l_pend_tail
== zr
->v4l_pend_head
) {
732 zr36057_set_memgrab(zr
, 0);
733 if (zr
->v4l_buffers
.active
== ZORAN_ACTIVE
) {
734 fh
->v4l_buffers
.active
= zr
->v4l_buffers
.active
=
736 zr
->v4l_buffers
.allocated
= 0;
740 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
746 * Queue a MJPEG buffer for capture/playback
750 zoran_jpg_queue_frame (struct file
*file
,
752 enum zoran_codec_mode mode
)
754 struct zoran_fh
*fh
= file
->private_data
;
755 struct zoran
*zr
= fh
->zr
;
759 /* Check if buffers are allocated */
760 if (!fh
->jpg_buffers
.allocated
) {
763 "%s: jpg_queue_frame() - buffers not yet allocated\n",
768 /* No grabbing outside the buffer range! */
769 if (num
>= fh
->jpg_buffers
.num_buffers
|| num
< 0) {
772 "%s: jpg_queue_frame() - buffer %d out of range\n",
773 ZR_DEVNAME(zr
), num
);
777 /* what is the codec mode right now? */
778 if (zr
->codec_mode
== BUZ_MODE_IDLE
) {
779 zr
->jpg_settings
= fh
->jpg_settings
;
780 } else if (zr
->codec_mode
!= mode
) {
781 /* wrong codec mode active - invalid */
784 "%s: jpg_queue_frame() - codec in wrong mode\n",
789 if (fh
->jpg_buffers
.active
== ZORAN_FREE
) {
790 if (zr
->jpg_buffers
.active
== ZORAN_FREE
) {
791 zr
->jpg_buffers
= fh
->jpg_buffers
;
792 fh
->jpg_buffers
.active
= ZORAN_ACTIVE
;
796 "%s: jpg_queue_frame() - another session is already capturing\n",
802 if (!res
&& zr
->codec_mode
== BUZ_MODE_IDLE
) {
803 /* Ok load up the jpeg codec */
804 zr36057_enable_jpg(zr
, mode
);
807 spin_lock_irqsave(&zr
->spinlock
, flags
);
810 switch (zr
->jpg_buffers
.buffer
[num
].state
) {
814 "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
817 /* since there is at least one unused buffer there's room for at
818 *least one more pend[] entry */
819 zr
->jpg_pend
[zr
->jpg_que_head
++ & BUZ_MASK_FRAME
] =
821 zr
->jpg_buffers
.buffer
[num
].state
= BUZ_STATE_PEND
;
822 fh
->jpg_buffers
.buffer
[num
] =
823 zr
->jpg_buffers
.buffer
[num
];
824 zoran_feed_stat_com(zr
);
829 if (zr
->jpg_buffers
.active
== ZORAN_FREE
) {
830 fh
->jpg_buffers
.active
= ZORAN_FREE
;
831 zr
->jpg_buffers
.allocated
= 0;
833 res
= -EBUSY
; /* what are you doing? */
838 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
840 if (!res
&& zr
->jpg_buffers
.active
== ZORAN_FREE
) {
841 zr
->jpg_buffers
.active
= fh
->jpg_buffers
.active
;
848 jpg_qbuf (struct file
*file
,
850 enum zoran_codec_mode mode
)
852 struct zoran_fh
*fh
= file
->private_data
;
853 struct zoran
*zr
= fh
->zr
;
856 /* Does the user want to stop streaming? */
858 if (zr
->codec_mode
== mode
) {
859 if (fh
->jpg_buffers
.active
== ZORAN_FREE
) {
862 "%s: jpg_qbuf(-1) - session not active\n",
866 fh
->jpg_buffers
.active
= zr
->jpg_buffers
.active
=
868 zr
->jpg_buffers
.allocated
= 0;
869 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
874 "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
880 if ((res
= zoran_jpg_queue_frame(file
, frame
, mode
)))
883 /* Start the jpeg codec when the first frame is queued */
884 if (!res
&& zr
->jpg_que_head
== 1)
891 * Sync on a MJPEG buffer
895 jpg_sync (struct file
*file
,
896 struct zoran_sync
*bs
)
898 struct zoran_fh
*fh
= file
->private_data
;
899 struct zoran
*zr
= fh
->zr
;
903 if (fh
->jpg_buffers
.active
== ZORAN_FREE
) {
906 "%s: jpg_sync() - capture is not currently active\n",
910 if (zr
->codec_mode
!= BUZ_MODE_MOTION_DECOMPRESS
&&
911 zr
->codec_mode
!= BUZ_MODE_MOTION_COMPRESS
) {
914 "%s: jpg_sync() - codec not in streaming mode\n",
918 if (!wait_event_interruptible_timeout(zr
->jpg_capq
,
919 (zr
->jpg_que_tail
!= zr
->jpg_dma_tail
||
920 zr
->jpg_dma_tail
== zr
->jpg_dma_head
),
924 btand(~ZR36057_JMC_Go_en
, ZR36057_JMC
);
926 zr
->codec
->control(zr
->codec
, CODEC_G_STATUS
,
930 "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
931 ZR_DEVNAME(zr
), isr
);
936 if (signal_pending(current
))
939 spin_lock_irqsave(&zr
->spinlock
, flags
);
941 if (zr
->jpg_dma_tail
!= zr
->jpg_dma_head
)
942 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
++ & BUZ_MASK_FRAME
];
944 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
946 /* buffer should now be in BUZ_STATE_DONE */
947 if (zr
->jpg_buffers
.buffer
[frame
].state
!= BUZ_STATE_DONE
)
949 KERN_ERR
"%s: jpg_sync() - internal state error\n",
952 *bs
= zr
->jpg_buffers
.buffer
[frame
].bs
;
954 zr
->jpg_buffers
.buffer
[frame
].state
= BUZ_STATE_USER
;
955 fh
->jpg_buffers
.buffer
[frame
] = zr
->jpg_buffers
.buffer
[frame
];
957 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
963 zoran_open_init_session (struct file
*file
)
966 struct zoran_fh
*fh
= file
->private_data
;
967 struct zoran
*zr
= fh
->zr
;
969 /* Per default, map the V4L Buffers */
970 fh
->map_mode
= ZORAN_MAP_MODE_RAW
;
972 /* take over the card's current settings */
973 fh
->overlay_settings
= zr
->overlay_settings
;
974 fh
->overlay_settings
.is_set
= 0;
975 fh
->overlay_settings
.format
= zr
->overlay_settings
.format
;
976 fh
->overlay_active
= ZORAN_FREE
;
979 fh
->v4l_settings
= zr
->v4l_settings
;
982 memset(&fh
->v4l_buffers
, 0, sizeof(struct zoran_v4l_struct
));
983 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
984 fh
->v4l_buffers
.buffer
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
985 fh
->v4l_buffers
.buffer
[i
].bs
.frame
= i
;
987 fh
->v4l_buffers
.allocated
= 0;
988 fh
->v4l_buffers
.active
= ZORAN_FREE
;
989 fh
->v4l_buffers
.buffer_size
= v4l_bufsize
;
990 fh
->v4l_buffers
.num_buffers
= v4l_nbufs
;
993 fh
->jpg_settings
= zr
->jpg_settings
;
996 memset(&fh
->jpg_buffers
, 0, sizeof(struct zoran_jpg_struct
));
997 for (i
= 0; i
< BUZ_MAX_FRAME
; i
++) {
998 fh
->jpg_buffers
.buffer
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
999 fh
->jpg_buffers
.buffer
[i
].bs
.frame
= i
;
1001 fh
->jpg_buffers
.need_contiguous
= zr
->jpg_buffers
.need_contiguous
;
1002 fh
->jpg_buffers
.allocated
= 0;
1003 fh
->jpg_buffers
.active
= ZORAN_FREE
;
1004 fh
->jpg_buffers
.buffer_size
= jpg_bufsize
;
1005 fh
->jpg_buffers
.num_buffers
= jpg_nbufs
;
1009 zoran_close_end_session (struct file
*file
)
1011 struct zoran_fh
*fh
= file
->private_data
;
1012 struct zoran
*zr
= fh
->zr
;
1015 if (fh
->overlay_active
!= ZORAN_FREE
) {
1016 fh
->overlay_active
= zr
->overlay_active
= ZORAN_FREE
;
1017 zr
->v4l_overlay_active
= 0;
1018 if (!zr
->v4l_memgrab_active
)
1019 zr36057_overlay(zr
, 0);
1020 zr
->overlay_mask
= NULL
;
1024 if (fh
->v4l_buffers
.active
!= ZORAN_FREE
) {
1025 unsigned long flags
;
1027 spin_lock_irqsave(&zr
->spinlock
, flags
);
1028 zr36057_set_memgrab(zr
, 0);
1029 zr
->v4l_buffers
.allocated
= 0;
1030 zr
->v4l_buffers
.active
= fh
->v4l_buffers
.active
=
1032 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
1036 if (fh
->v4l_buffers
.allocated
)
1037 v4l_fbuffer_free(file
);
1040 if (fh
->jpg_buffers
.active
!= ZORAN_FREE
) {
1041 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
1042 zr
->jpg_buffers
.allocated
= 0;
1043 zr
->jpg_buffers
.active
= fh
->jpg_buffers
.active
=
1048 if (fh
->jpg_buffers
.allocated
)
1049 jpg_fbuffer_free(file
);
1053 * Open a zoran card. Right now the flags stuff is just playing
1056 static int zoran_open(struct file
*file
)
1058 struct zoran
*zr
= video_drvdata(file
);
1059 struct zoran_fh
*fh
;
1060 int res
, first_open
= 0;
1062 dprintk(2, KERN_INFO
"%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
1063 ZR_DEVNAME(zr
), current
->comm
, task_pid_nr(current
), zr
->user
+ 1);
1067 /* see fs/device.c - the kernel already locks during open(),
1068 * so locking ourselves only causes deadlocks */
1069 /*mutex_lock(&zr->resource_lock);*/
1071 if (zr
->user
>= 2048) {
1072 dprintk(1, KERN_ERR
"%s: too many users (%d) on device\n",
1073 ZR_DEVNAME(zr
), zr
->user
);
1080 KERN_ERR
"%s: no TV decoder loaded for device!\n",
1086 if (!try_module_get(zr
->decoder
->driver
->driver
.owner
)) {
1089 "%s: failed to grab ownership of video decoder\n",
1095 !try_module_get(zr
->encoder
->driver
->driver
.owner
)) {
1098 "%s: failed to grab ownership of video encoder\n",
1104 /* now, create the open()-specific file_ops struct */
1105 fh
= kzalloc(sizeof(struct zoran_fh
), GFP_KERNEL
);
1109 "%s: zoran_open() - allocation of zoran_fh failed\n",
1114 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
1115 * on norm-change! */
1117 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL
);
1118 if (!fh
->overlay_mask
) {
1121 "%s: zoran_open() - allocation of overlay_mask failed\n",
1127 if (zr
->user
++ == 0)
1130 /*mutex_unlock(&zr->resource_lock);*/
1132 /* default setup - TODO: look at flags */
1133 if (first_open
) { /* First device open */
1134 zr36057_restart(zr
);
1135 zoran_open_init_params(zr
);
1136 zoran_init_hardware(zr
);
1138 btor(ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
1141 /* set file_ops stuff */
1142 file
->private_data
= fh
;
1144 zoran_open_init_session(file
);
1153 module_put(zr
->encoder
->driver
->driver
.owner
);
1155 module_put(zr
->decoder
->driver
->driver
.owner
);
1159 dprintk(2, KERN_INFO
"%s: open failed (%d), users(-)=%d\n",
1160 ZR_DEVNAME(zr
), res
, zr
->user
);
1166 zoran_close(struct file
*file
)
1168 struct zoran_fh
*fh
= file
->private_data
;
1169 struct zoran
*zr
= fh
->zr
;
1171 dprintk(2, KERN_INFO
"%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
1172 ZR_DEVNAME(zr
), current
->comm
, task_pid_nr(current
), zr
->user
- 1);
1174 /* kernel locks (fs/device.c), so don't do that ourselves
1175 * (prevents deadlocks) */
1176 /*mutex_lock(&zr->resource_lock);*/
1178 zoran_close_end_session(file
);
1180 if (zr
->user
-- == 1) { /* Last process */
1181 /* Clean up JPEG process */
1182 wake_up_interruptible(&zr
->jpg_capq
);
1183 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
1184 zr
->jpg_buffers
.allocated
= 0;
1185 zr
->jpg_buffers
.active
= ZORAN_FREE
;
1187 /* disable interrupts */
1188 btand(~ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
1190 if (zr36067_debug
> 1)
1191 print_interrupts(zr
);
1194 zr
->v4l_overlay_active
= 0;
1195 zr36057_overlay(zr
, 0);
1196 zr
->overlay_mask
= NULL
;
1199 wake_up_interruptible(&zr
->v4l_capq
);
1200 zr36057_set_memgrab(zr
, 0);
1201 zr
->v4l_buffers
.allocated
= 0;
1202 zr
->v4l_buffers
.active
= ZORAN_FREE
;
1203 zoran_set_pci_master(zr
, 0);
1205 if (!pass_through
) { /* Switch to color bar */
1206 int zero
= 0, two
= 2;
1207 decoder_command(zr
, DECODER_ENABLE_OUTPUT
, &zero
);
1208 encoder_command(zr
, ENCODER_SET_INPUT
, &two
);
1212 file
->private_data
= NULL
;
1213 kfree(fh
->overlay_mask
);
1216 /* release locks on the i2c modules */
1217 module_put(zr
->decoder
->driver
->driver
.owner
);
1219 module_put(zr
->encoder
->driver
->driver
.owner
);
1221 /*mutex_unlock(&zr->resource_lock);*/
1223 dprintk(4, KERN_INFO
"%s: zoran_close() done\n", ZR_DEVNAME(zr
));
1230 zoran_read (struct file
*file
,
1235 /* we simply don't support read() (yet)... */
1241 zoran_write (struct file
*file
,
1242 const char __user
*data
,
1246 /* ...and the same goes for write() */
1252 setup_fbuffer (struct file
*file
,
1254 const struct zoran_format
*fmt
,
1259 struct zoran_fh
*fh
= file
->private_data
;
1260 struct zoran
*zr
= fh
->zr
;
1262 /* (Ronald) v4l/v4l2 guidelines */
1263 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
1266 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1267 ALi Magik (that needs very low latency while the card needs a
1268 higher value always) */
1270 if (pci_pci_problems
& (PCIPCI_FAIL
| PCIAGP_FAIL
| PCIPCI_ALIMAGIK
))
1273 /* we need a bytesperline value, even if not given */
1275 bytesperline
= width
* ((fmt
->depth
+ 7) & ~7) / 8;
1278 if (zr
->overlay_active
) {
1279 /* dzjee... stupid users... don't even bother to turn off
1280 * overlay before changing the memory location...
1281 * normally, we would return errors here. However, one of
1282 * the tools that does this is... xawtv! and since xawtv
1283 * is used by +/- 99% of the users, we'd rather be user-
1284 * friendly and silently do as if nothing went wrong */
1287 "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
1289 zr36057_overlay(zr
, 0);
1293 if (!(fmt
->flags
& ZORAN_FORMAT_OVERLAY
)) {
1296 "%s: setup_fbuffer() - no valid overlay format given\n",
1300 if (height
<= 0 || width
<= 0 || bytesperline
<= 0) {
1303 "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
1304 ZR_DEVNAME(zr
), width
, height
, bytesperline
);
1307 if (bytesperline
& 3) {
1310 "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
1311 ZR_DEVNAME(zr
), bytesperline
);
1315 zr
->buffer
.base
= (void *) ((unsigned long) base
& ~3);
1316 zr
->buffer
.height
= height
;
1317 zr
->buffer
.width
= width
;
1318 zr
->buffer
.depth
= fmt
->depth
;
1319 zr
->overlay_settings
.format
= fmt
;
1320 zr
->buffer
.bytesperline
= bytesperline
;
1322 /* The user should set new window parameters */
1323 zr
->overlay_settings
.is_set
= 0;
1330 setup_window (struct file
*file
,
1335 struct video_clip __user
*clips
,
1337 void __user
*bitmap
)
1339 struct zoran_fh
*fh
= file
->private_data
;
1340 struct zoran
*zr
= fh
->zr
;
1341 struct video_clip
*vcp
= NULL
;
1345 if (!zr
->buffer
.base
) {
1348 "%s: setup_window() - frame buffer has to be set first\n",
1353 if (!fh
->overlay_settings
.format
) {
1356 "%s: setup_window() - no overlay format set\n",
1362 * The video front end needs 4-byte alinged line sizes, we correct that
1363 * silently here if necessary
1365 if (zr
->buffer
.depth
== 15 || zr
->buffer
.depth
== 16) {
1366 end
= (x
+ width
) & ~1; /* round down */
1367 x
= (x
+ 1) & ~1; /* round up */
1371 if (zr
->buffer
.depth
== 24) {
1372 end
= (x
+ width
) & ~3; /* round down */
1373 x
= (x
+ 3) & ~3; /* round up */
1377 if (width
> BUZ_MAX_WIDTH
)
1378 width
= BUZ_MAX_WIDTH
;
1379 if (height
> BUZ_MAX_HEIGHT
)
1380 height
= BUZ_MAX_HEIGHT
;
1382 /* Check for vaild parameters */
1383 if (width
< BUZ_MIN_WIDTH
|| height
< BUZ_MIN_HEIGHT
||
1384 width
> BUZ_MAX_WIDTH
|| height
> BUZ_MAX_HEIGHT
) {
1387 "%s: setup_window() - width = %d or height = %d invalid\n",
1388 ZR_DEVNAME(zr
), width
, height
);
1392 fh
->overlay_settings
.x
= x
;
1393 fh
->overlay_settings
.y
= y
;
1394 fh
->overlay_settings
.width
= width
;
1395 fh
->overlay_settings
.height
= height
;
1396 fh
->overlay_settings
.clipcount
= clipcount
;
1399 * If an overlay is running, we have to switch it off
1400 * and switch it on again in order to get the new settings in effect.
1402 * We also want to avoid that the overlay mask is written
1403 * when an overlay is running.
1406 on
= zr
->v4l_overlay_active
&& !zr
->v4l_memgrab_active
&&
1407 zr
->overlay_active
!= ZORAN_FREE
&&
1408 fh
->overlay_active
!= ZORAN_FREE
;
1410 zr36057_overlay(zr
, 0);
1413 * Write the overlay mask if clips are wanted.
1414 * We prefer a bitmap.
1417 /* fake value - it just means we want clips */
1418 fh
->overlay_settings
.clipcount
= 1;
1420 if (copy_from_user(fh
->overlay_mask
, bitmap
,
1421 (width
* height
+ 7) / 8)) {
1424 } else if (clipcount
> 0) {
1425 /* write our own bitmap from the clips */
1426 vcp
= vmalloc(sizeof(struct video_clip
) * (clipcount
+ 4));
1430 "%s: setup_window() - Alloc of clip mask failed\n",
1435 (vcp
, clips
, sizeof(struct video_clip
) * clipcount
)) {
1439 write_overlay_mask(file
, vcp
, clipcount
);
1443 fh
->overlay_settings
.is_set
= 1;
1444 if (fh
->overlay_active
!= ZORAN_FREE
&&
1445 zr
->overlay_active
!= ZORAN_FREE
)
1446 zr
->overlay_settings
= fh
->overlay_settings
;
1449 zr36057_overlay(zr
, 1);
1451 /* Make sure the changes come into effect */
1452 return wait_grab_pending(zr
);
1456 setup_overlay (struct file
*file
,
1459 struct zoran_fh
*fh
= file
->private_data
;
1460 struct zoran
*zr
= fh
->zr
;
1462 /* If there is nothing to do, return immediatly */
1463 if ((on
&& fh
->overlay_active
!= ZORAN_FREE
) ||
1464 (!on
&& fh
->overlay_active
== ZORAN_FREE
))
1467 /* check whether we're touching someone else's overlay */
1468 if (on
&& zr
->overlay_active
!= ZORAN_FREE
&&
1469 fh
->overlay_active
== ZORAN_FREE
) {
1472 "%s: setup_overlay() - overlay is already active for another session\n",
1476 if (!on
&& zr
->overlay_active
!= ZORAN_FREE
&&
1477 fh
->overlay_active
== ZORAN_FREE
) {
1480 "%s: setup_overlay() - you cannot cancel someone else's session\n",
1486 zr
->overlay_active
= fh
->overlay_active
= ZORAN_FREE
;
1487 zr
->v4l_overlay_active
= 0;
1488 /* When a grab is running, the video simply
1489 * won't be switched on any more */
1490 if (!zr
->v4l_memgrab_active
)
1491 zr36057_overlay(zr
, 0);
1492 zr
->overlay_mask
= NULL
;
1494 if (!zr
->buffer
.base
|| !fh
->overlay_settings
.is_set
) {
1497 "%s: setup_overlay() - buffer or window not set\n",
1501 if (!fh
->overlay_settings
.format
) {
1504 "%s: setup_overlay() - no overlay format set\n",
1508 zr
->overlay_active
= fh
->overlay_active
= ZORAN_LOCKED
;
1509 zr
->v4l_overlay_active
= 1;
1510 zr
->overlay_mask
= fh
->overlay_mask
;
1511 zr
->overlay_settings
= fh
->overlay_settings
;
1512 if (!zr
->v4l_memgrab_active
)
1513 zr36057_overlay(zr
, 1);
1514 /* When a grab is running, the video will be
1515 * switched on when grab is finished */
1518 /* Make sure the changes come into effect */
1519 return wait_grab_pending(zr
);
1522 /* get the status of a buffer in the clients buffer queue */
1524 zoran_v4l2_buffer_status (struct file
*file
,
1525 struct v4l2_buffer
*buf
,
1528 struct zoran_fh
*fh
= file
->private_data
;
1529 struct zoran
*zr
= fh
->zr
;
1531 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1533 switch (fh
->map_mode
) {
1534 case ZORAN_MAP_MODE_RAW
:
1537 if (num
< 0 || num
>= fh
->v4l_buffers
.num_buffers
||
1538 !fh
->v4l_buffers
.allocated
) {
1541 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1546 buf
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1547 buf
->length
= fh
->v4l_buffers
.buffer_size
;
1550 buf
->bytesused
= fh
->v4l_buffers
.buffer
[num
].bs
.length
;
1551 if (fh
->v4l_buffers
.buffer
[num
].state
== BUZ_STATE_DONE
||
1552 fh
->v4l_buffers
.buffer
[num
].state
== BUZ_STATE_USER
) {
1553 buf
->sequence
= fh
->v4l_buffers
.buffer
[num
].bs
.seq
;
1554 buf
->flags
|= V4L2_BUF_FLAG_DONE
;
1556 fh
->v4l_buffers
.buffer
[num
].bs
.timestamp
;
1558 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1561 if (fh
->v4l_settings
.height
<= BUZ_MAX_HEIGHT
/ 2)
1562 buf
->field
= V4L2_FIELD_TOP
;
1564 buf
->field
= V4L2_FIELD_INTERLACED
;
1568 case ZORAN_MAP_MODE_JPG_REC
:
1569 case ZORAN_MAP_MODE_JPG_PLAY
:
1572 if (num
< 0 || num
>= fh
->jpg_buffers
.num_buffers
||
1573 !fh
->jpg_buffers
.allocated
) {
1576 "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
1581 buf
->type
= (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
) ?
1582 V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1583 V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1584 buf
->length
= fh
->jpg_buffers
.buffer_size
;
1586 /* these variables are only written after frame has been captured */
1587 if (fh
->jpg_buffers
.buffer
[num
].state
== BUZ_STATE_DONE
||
1588 fh
->jpg_buffers
.buffer
[num
].state
== BUZ_STATE_USER
) {
1589 buf
->sequence
= fh
->jpg_buffers
.buffer
[num
].bs
.seq
;
1591 fh
->jpg_buffers
.buffer
[num
].bs
.timestamp
;
1593 fh
->jpg_buffers
.buffer
[num
].bs
.length
;
1594 buf
->flags
|= V4L2_BUF_FLAG_DONE
;
1596 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1599 /* which fields are these? */
1600 if (fh
->jpg_settings
.TmpDcm
!= 1)
1603 odd_even
? V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
;
1607 odd_even
? V4L2_FIELD_SEQ_TB
:
1616 "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
1617 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
1621 buf
->memory
= V4L2_MEMORY_MMAP
;
1623 buf
->m
.offset
= buf
->length
* num
;
1629 zoran_set_norm (struct zoran
*zr
,
1630 int norm
) /* VIDEO_MODE_* */
1632 int norm_encoder
, on
;
1634 if (zr
->v4l_buffers
.active
!= ZORAN_FREE
||
1635 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
1638 "%s: set_norm() called while in playback/capture mode\n",
1643 if (lock_norm
&& norm
!= zr
->norm
) {
1644 if (lock_norm
> 1) {
1647 "%s: set_norm() - TV standard is locked, can not switch norm\n",
1653 "%s: set_norm() - TV standard is locked, norm was not changed\n",
1659 if (norm
!= VIDEO_MODE_AUTO
&&
1660 (norm
< 0 || norm
>= zr
->card
.norms
||
1661 !zr
->card
.tvn
[norm
])) {
1663 KERN_ERR
"%s: set_norm() - unsupported norm %d\n",
1664 ZR_DEVNAME(zr
), norm
);
1668 if (norm
== VIDEO_MODE_AUTO
) {
1671 /* if we have autodetect, ... */
1672 struct video_decoder_capability caps
;
1673 decoder_command(zr
, DECODER_GET_CAPABILITIES
, &caps
);
1674 if (!(caps
.flags
& VIDEO_DECODER_AUTO
)) {
1675 dprintk(1, KERN_ERR
"%s: norm=auto unsupported\n",
1680 decoder_command(zr
, DECODER_SET_NORM
, &norm
);
1682 /* let changes come into effect */
1685 decoder_command(zr
, DECODER_GET_STATUS
, &status
);
1686 if (!(status
& DECODER_STATUS_GOOD
)) {
1689 "%s: set_norm() - no norm detected\n",
1692 decoder_command(zr
, DECODER_SET_NORM
, &zr
->norm
);
1696 if (status
& DECODER_STATUS_NTSC
)
1697 norm
= VIDEO_MODE_NTSC
;
1698 else if (status
& DECODER_STATUS_SECAM
)
1699 norm
= VIDEO_MODE_SECAM
;
1701 norm
= VIDEO_MODE_PAL
;
1703 zr
->timing
= zr
->card
.tvn
[norm
];
1704 norm_encoder
= norm
;
1706 /* We switch overlay off and on since a change in the
1707 * norm needs different VFE settings */
1708 on
= zr
->overlay_active
&& !zr
->v4l_memgrab_active
;
1710 zr36057_overlay(zr
, 0);
1712 decoder_command(zr
, DECODER_SET_NORM
, &norm
);
1713 encoder_command(zr
, ENCODER_SET_NORM
, &norm_encoder
);
1716 zr36057_overlay(zr
, 1);
1718 /* Make sure the changes come into effect */
1725 zoran_set_input (struct zoran
*zr
,
1730 if (input
== zr
->input
) {
1734 if (zr
->v4l_buffers
.active
!= ZORAN_FREE
||
1735 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
1738 "%s: set_input() called while in playback/capture mode\n",
1743 if (input
< 0 || input
>= zr
->card
.inputs
) {
1746 "%s: set_input() - unnsupported input %d\n",
1747 ZR_DEVNAME(zr
), input
);
1751 realinput
= zr
->card
.input
[input
].muxsel
;
1754 decoder_command(zr
, DECODER_SET_INPUT
, &realinput
);
1763 static long zoran_default(struct file
*file
, void *__fh
, int cmd
, void *arg
)
1765 struct zoran_fh
*fh
= __fh
;
1766 struct zoran
*zr
= fh
->zr
;
1767 struct zoran_jpg_settings settings
;
1772 struct video_capability
*vcap
= arg
;
1774 dprintk(3, KERN_DEBUG
"%s: VIDIOCGCAP\n", ZR_DEVNAME(zr
));
1776 memset(vcap
, 0, sizeof(struct video_capability
));
1777 strncpy(vcap
->name
, ZR_DEVNAME(zr
), sizeof(vcap
->name
)-1);
1778 vcap
->type
= ZORAN_VID_TYPE
;
1780 vcap
->channels
= zr
->card
.inputs
;
1782 mutex_lock(&zr
->resource_lock
);
1783 vcap
->maxwidth
= BUZ_MAX_WIDTH
;
1784 vcap
->maxheight
= BUZ_MAX_HEIGHT
;
1785 vcap
->minwidth
= BUZ_MIN_WIDTH
;
1786 vcap
->minheight
= BUZ_MIN_HEIGHT
;
1787 mutex_unlock(&zr
->resource_lock
);
1794 struct video_channel
*vchan
= arg
;
1795 int channel
= vchan
->channel
;
1797 dprintk(3, KERN_DEBUG
"%s: VIDIOCGCHAN - channel=%d\n",
1798 ZR_DEVNAME(zr
), vchan
->channel
);
1800 memset(vchan
, 0, sizeof(struct video_channel
));
1801 if (channel
> zr
->card
.inputs
|| channel
< 0) {
1804 "%s: VIDIOCGCHAN on not existing channel %d\n",
1805 ZR_DEVNAME(zr
), channel
);
1809 strcpy(vchan
->name
, zr
->card
.input
[channel
].name
);
1813 vchan
->type
= VIDEO_TYPE_CAMERA
;
1814 mutex_lock(&zr
->resource_lock
);
1815 vchan
->norm
= zr
->norm
;
1816 mutex_unlock(&zr
->resource_lock
);
1817 vchan
->channel
= channel
;
1822 /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
1824 * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
1826 * * The famos BTTV driver has it implemented with a struct video_channel argument
1827 * * and we follow it for compatibility reasons
1829 * * BTW: this is the only way the user can set the norm!
1834 struct video_channel
*vchan
= arg
;
1839 "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
1840 ZR_DEVNAME(zr
), vchan
->channel
, vchan
->norm
);
1842 mutex_lock(&zr
->resource_lock
);
1843 if ((res
= zoran_set_input(zr
, vchan
->channel
)))
1844 goto schan_unlock_and_return
;
1845 if ((res
= zoran_set_norm(zr
, vchan
->norm
)))
1846 goto schan_unlock_and_return
;
1848 /* Make sure the changes come into effect */
1849 res
= wait_grab_pending(zr
);
1850 schan_unlock_and_return
:
1851 mutex_unlock(&zr
->resource_lock
);
1857 struct video_picture
*vpict
= arg
;
1859 dprintk(3, KERN_DEBUG
"%s: VIDIOCGPICT\n", ZR_DEVNAME(zr
));
1861 memset(vpict
, 0, sizeof(struct video_picture
));
1862 mutex_lock(&zr
->resource_lock
);
1863 vpict
->hue
= zr
->hue
;
1864 vpict
->brightness
= zr
->brightness
;
1865 vpict
->contrast
= zr
->contrast
;
1866 vpict
->colour
= zr
->saturation
;
1867 if (fh
->overlay_settings
.format
) {
1868 vpict
->depth
= fh
->overlay_settings
.format
->depth
;
1869 vpict
->palette
= fh
->overlay_settings
.format
->palette
;
1873 mutex_unlock(&zr
->resource_lock
);
1880 struct video_picture
*vpict
= arg
;
1885 "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
1886 ZR_DEVNAME(zr
), vpict
->brightness
, vpict
->hue
,
1887 vpict
->colour
, vpict
->contrast
, vpict
->depth
,
1890 for (i
= 0; i
< NUM_FORMATS
; i
++) {
1891 const struct zoran_format
*fmt
= &zoran_formats
[i
];
1893 if (fmt
->palette
!= -1 &&
1894 fmt
->flags
& ZORAN_FORMAT_OVERLAY
&&
1895 fmt
->palette
== vpict
->palette
&&
1896 fmt
->depth
== vpict
->depth
)
1899 if (i
== NUM_FORMATS
) {
1902 "%s: VIDIOCSPICT - Invalid palette %d\n",
1903 ZR_DEVNAME(zr
), vpict
->palette
);
1907 mutex_lock(&zr
->resource_lock
);
1909 decoder_command(zr
, DECODER_SET_PICTURE
, vpict
);
1911 zr
->hue
= vpict
->hue
;
1912 zr
->contrast
= vpict
->contrast
;
1913 zr
->saturation
= vpict
->colour
;
1914 zr
->brightness
= vpict
->brightness
;
1916 fh
->overlay_settings
.format
= &zoran_formats
[i
];
1918 mutex_unlock(&zr
->resource_lock
);
1927 dprintk(3, KERN_DEBUG
"%s: VIDIOCCAPTURE - on=%d\n",
1928 ZR_DEVNAME(zr
), *on
);
1930 mutex_lock(&zr
->resource_lock
);
1931 res
= setup_overlay(file
, *on
);
1932 mutex_unlock(&zr
->resource_lock
);
1939 struct video_window
*vwin
= arg
;
1941 dprintk(3, KERN_DEBUG
"%s: VIDIOCGWIN\n", ZR_DEVNAME(zr
));
1943 memset(vwin
, 0, sizeof(struct video_window
));
1944 mutex_lock(&zr
->resource_lock
);
1945 vwin
->x
= fh
->overlay_settings
.x
;
1946 vwin
->y
= fh
->overlay_settings
.y
;
1947 vwin
->width
= fh
->overlay_settings
.width
;
1948 vwin
->height
= fh
->overlay_settings
.height
;
1949 mutex_unlock(&zr
->resource_lock
);
1950 vwin
->clipcount
= 0;
1956 struct video_window
*vwin
= arg
;
1961 "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
1962 ZR_DEVNAME(zr
), vwin
->x
, vwin
->y
, vwin
->width
,
1963 vwin
->height
, vwin
->clipcount
);
1965 mutex_lock(&zr
->resource_lock
);
1967 setup_window(file
, vwin
->x
, vwin
->y
, vwin
->width
,
1968 vwin
->height
, vwin
->clips
,
1969 vwin
->clipcount
, NULL
);
1970 mutex_unlock(&zr
->resource_lock
);
1977 struct video_buffer
*vbuf
= arg
;
1979 dprintk(3, KERN_DEBUG
"%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr
));
1981 mutex_lock(&zr
->resource_lock
);
1983 mutex_unlock(&zr
->resource_lock
);
1989 struct video_buffer
*vbuf
= arg
;
1994 "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
1995 ZR_DEVNAME(zr
), vbuf
->base
, vbuf
->width
,
1996 vbuf
->height
, vbuf
->depth
, vbuf
->bytesperline
);
1998 for (i
= 0; i
< NUM_FORMATS
; i
++)
1999 if (zoran_formats
[i
].depth
== vbuf
->depth
)
2001 if (i
== NUM_FORMATS
) {
2004 "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
2005 ZR_DEVNAME(zr
), vbuf
->depth
);
2009 mutex_lock(&zr
->resource_lock
);
2011 setup_fbuffer(file
, vbuf
->base
, &zoran_formats
[i
],
2012 vbuf
->width
, vbuf
->height
,
2013 vbuf
->bytesperline
);
2014 mutex_unlock(&zr
->resource_lock
);
2021 int *frame
= arg
, res
;
2023 dprintk(3, KERN_DEBUG
"%s: VIDIOCSYNC - frame=%d\n",
2024 ZR_DEVNAME(zr
), *frame
);
2026 mutex_lock(&zr
->resource_lock
);
2027 res
= v4l_sync(file
, *frame
);
2028 mutex_unlock(&zr
->resource_lock
);
2030 zr
->v4l_sync_tail
++;
2034 case VIDIOCMCAPTURE
:
2036 struct video_mmap
*vmap
= arg
;
2041 "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
2042 ZR_DEVNAME(zr
), vmap
->frame
, vmap
->width
, vmap
->height
,
2045 mutex_lock(&zr
->resource_lock
);
2046 res
= v4l_grab(file
, vmap
);
2047 mutex_unlock(&zr
->resource_lock
);
2054 struct video_mbuf
*vmbuf
= arg
;
2057 dprintk(3, KERN_DEBUG
"%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr
));
2060 fh
->v4l_buffers
.num_buffers
*
2061 fh
->v4l_buffers
.buffer_size
;
2062 vmbuf
->frames
= fh
->v4l_buffers
.num_buffers
;
2063 for (i
= 0; i
< vmbuf
->frames
; i
++) {
2065 i
* fh
->v4l_buffers
.buffer_size
;
2068 mutex_lock(&zr
->resource_lock
);
2070 if (fh
->jpg_buffers
.allocated
|| fh
->v4l_buffers
.allocated
) {
2073 "%s: VIDIOCGMBUF - buffers already allocated\n",
2076 goto v4l1reqbuf_unlock_and_return
;
2079 if (v4l_fbuffer_alloc(file
)) {
2081 goto v4l1reqbuf_unlock_and_return
;
2084 /* The next mmap will map the V4L buffers */
2085 fh
->map_mode
= ZORAN_MAP_MODE_RAW
;
2086 v4l1reqbuf_unlock_and_return
:
2087 mutex_unlock(&zr
->resource_lock
);
2094 struct video_unit
*vunit
= arg
;
2096 dprintk(3, KERN_DEBUG
"%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr
));
2098 vunit
->video
= zr
->video_dev
->minor
;
2099 vunit
->vbi
= VIDEO_NO_UNIT
;
2100 vunit
->radio
= VIDEO_NO_UNIT
;
2101 vunit
->audio
= VIDEO_NO_UNIT
;
2102 vunit
->teletext
= VIDEO_NO_UNIT
;
2108 * RJ: In principal we could support subcaptures for V4L grabbing.
2109 * Not even the famous BTTV driver has them, however.
2110 * If there should be a strong demand, one could consider
2111 * to implement them.
2113 case VIDIOCGCAPTURE
:
2115 dprintk(3, KERN_ERR
"%s: VIDIOCGCAPTURE not supported\n",
2120 case VIDIOCSCAPTURE
:
2122 dprintk(3, KERN_ERR
"%s: VIDIOCSCAPTURE not supported\n",
2127 case BUZIOC_G_PARAMS
:
2129 struct zoran_params
*bparams
= arg
;
2131 dprintk(3, KERN_DEBUG
"%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr
));
2133 memset(bparams
, 0, sizeof(struct zoran_params
));
2134 bparams
->major_version
= MAJOR_VERSION
;
2135 bparams
->minor_version
= MINOR_VERSION
;
2137 mutex_lock(&zr
->resource_lock
);
2139 bparams
->norm
= zr
->norm
;
2140 bparams
->input
= zr
->input
;
2142 bparams
->decimation
= fh
->jpg_settings
.decimation
;
2143 bparams
->HorDcm
= fh
->jpg_settings
.HorDcm
;
2144 bparams
->VerDcm
= fh
->jpg_settings
.VerDcm
;
2145 bparams
->TmpDcm
= fh
->jpg_settings
.TmpDcm
;
2146 bparams
->field_per_buff
= fh
->jpg_settings
.field_per_buff
;
2147 bparams
->img_x
= fh
->jpg_settings
.img_x
;
2148 bparams
->img_y
= fh
->jpg_settings
.img_y
;
2149 bparams
->img_width
= fh
->jpg_settings
.img_width
;
2150 bparams
->img_height
= fh
->jpg_settings
.img_height
;
2151 bparams
->odd_even
= fh
->jpg_settings
.odd_even
;
2153 bparams
->quality
= fh
->jpg_settings
.jpg_comp
.quality
;
2154 bparams
->APPn
= fh
->jpg_settings
.jpg_comp
.APPn
;
2155 bparams
->APP_len
= fh
->jpg_settings
.jpg_comp
.APP_len
;
2156 memcpy(bparams
->APP_data
,
2157 fh
->jpg_settings
.jpg_comp
.APP_data
,
2158 sizeof(bparams
->APP_data
));
2159 bparams
->COM_len
= zr
->jpg_settings
.jpg_comp
.COM_len
;
2160 memcpy(bparams
->COM_data
,
2161 fh
->jpg_settings
.jpg_comp
.COM_data
,
2162 sizeof(bparams
->COM_data
));
2163 bparams
->jpeg_markers
=
2164 fh
->jpg_settings
.jpg_comp
.jpeg_markers
;
2166 mutex_unlock(&zr
->resource_lock
);
2168 bparams
->VFIFO_FB
= 0;
2173 case BUZIOC_S_PARAMS
:
2175 struct zoran_params
*bparams
= arg
;
2178 dprintk(3, KERN_DEBUG
"%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr
));
2180 settings
.decimation
= bparams
->decimation
;
2181 settings
.HorDcm
= bparams
->HorDcm
;
2182 settings
.VerDcm
= bparams
->VerDcm
;
2183 settings
.TmpDcm
= bparams
->TmpDcm
;
2184 settings
.field_per_buff
= bparams
->field_per_buff
;
2185 settings
.img_x
= bparams
->img_x
;
2186 settings
.img_y
= bparams
->img_y
;
2187 settings
.img_width
= bparams
->img_width
;
2188 settings
.img_height
= bparams
->img_height
;
2189 settings
.odd_even
= bparams
->odd_even
;
2191 settings
.jpg_comp
.quality
= bparams
->quality
;
2192 settings
.jpg_comp
.APPn
= bparams
->APPn
;
2193 settings
.jpg_comp
.APP_len
= bparams
->APP_len
;
2194 memcpy(settings
.jpg_comp
.APP_data
, bparams
->APP_data
,
2195 sizeof(bparams
->APP_data
));
2196 settings
.jpg_comp
.COM_len
= bparams
->COM_len
;
2197 memcpy(settings
.jpg_comp
.COM_data
, bparams
->COM_data
,
2198 sizeof(bparams
->COM_data
));
2199 settings
.jpg_comp
.jpeg_markers
= bparams
->jpeg_markers
;
2201 mutex_lock(&zr
->resource_lock
);
2203 if (zr
->codec_mode
!= BUZ_MODE_IDLE
) {
2206 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
2209 goto sparams_unlock_and_return
;
2212 /* Check the params first before overwriting our
2214 if (zoran_check_jpg_settings(zr
, &settings
)) {
2216 goto sparams_unlock_and_return
;
2219 fh
->jpg_settings
= settings
;
2220 sparams_unlock_and_return
:
2221 mutex_unlock(&zr
->resource_lock
);
2226 case BUZIOC_REQBUFS
:
2228 struct zoran_requestbuffers
*breq
= arg
;
2233 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
2234 ZR_DEVNAME(zr
), breq
->count
, breq
->size
);
2236 /* Enforce reasonable lower and upper limits */
2237 if (breq
->count
< 4)
2238 breq
->count
= 4; /* Could be choosen smaller */
2239 if (breq
->count
> jpg_nbufs
)
2240 breq
->count
= jpg_nbufs
;
2241 breq
->size
= PAGE_ALIGN(breq
->size
);
2242 if (breq
->size
< 8192)
2243 breq
->size
= 8192; /* Arbitrary */
2244 /* breq->size is limited by 1 page for the stat_com
2245 * tables to a Maximum of 2 MB */
2246 if (breq
->size
> jpg_bufsize
)
2247 breq
->size
= jpg_bufsize
;
2249 mutex_lock(&zr
->resource_lock
);
2251 if (fh
->jpg_buffers
.allocated
|| fh
->v4l_buffers
.allocated
) {
2254 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
2257 goto jpgreqbuf_unlock_and_return
;
2260 fh
->jpg_buffers
.num_buffers
= breq
->count
;
2261 fh
->jpg_buffers
.buffer_size
= breq
->size
;
2263 if (jpg_fbuffer_alloc(file
)) {
2265 goto jpgreqbuf_unlock_and_return
;
2268 /* The next mmap will map the MJPEG buffers - could
2269 * also be *_PLAY, but it doesn't matter here */
2270 fh
->map_mode
= ZORAN_MAP_MODE_JPG_REC
;
2271 jpgreqbuf_unlock_and_return
:
2272 mutex_unlock(&zr
->resource_lock
);
2277 case BUZIOC_QBUF_CAPT
:
2279 int *frame
= arg
, res
;
2281 dprintk(3, KERN_DEBUG
"%s: BUZIOC_QBUF_CAPT - frame=%d\n",
2282 ZR_DEVNAME(zr
), *frame
);
2284 mutex_lock(&zr
->resource_lock
);
2285 res
= jpg_qbuf(file
, *frame
, BUZ_MODE_MOTION_COMPRESS
);
2286 mutex_unlock(&zr
->resource_lock
);
2291 case BUZIOC_QBUF_PLAY
:
2293 int *frame
= arg
, res
;
2295 dprintk(3, KERN_DEBUG
"%s: BUZIOC_QBUF_PLAY - frame=%d\n",
2296 ZR_DEVNAME(zr
), *frame
);
2298 mutex_lock(&zr
->resource_lock
);
2299 res
= jpg_qbuf(file
, *frame
, BUZ_MODE_MOTION_DECOMPRESS
);
2300 mutex_unlock(&zr
->resource_lock
);
2307 struct zoran_sync
*bsync
= arg
;
2310 dprintk(3, KERN_DEBUG
"%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr
));
2312 mutex_lock(&zr
->resource_lock
);
2313 res
= jpg_sync(file
, bsync
);
2314 mutex_unlock(&zr
->resource_lock
);
2319 case BUZIOC_G_STATUS
:
2321 struct zoran_status
*bstat
= arg
;
2322 int norm
, input
, status
, res
= 0;
2324 dprintk(3, KERN_DEBUG
"%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr
));
2326 if (zr
->codec_mode
!= BUZ_MODE_IDLE
) {
2329 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
2334 input
= zr
->card
.input
[bstat
->input
].muxsel
;
2335 norm
= VIDEO_MODE_AUTO
;
2337 mutex_lock(&zr
->resource_lock
);
2339 if (zr
->codec_mode
!= BUZ_MODE_IDLE
) {
2342 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
2345 goto gstat_unlock_and_return
;
2348 decoder_command(zr
, DECODER_SET_INPUT
, &input
);
2349 decoder_command(zr
, DECODER_SET_NORM
, &norm
);
2351 /* sleep 1 second */
2354 /* Get status of video decoder */
2355 decoder_command(zr
, DECODER_GET_STATUS
, &status
);
2357 /* restore previous input and norm */
2358 input
= zr
->card
.input
[zr
->input
].muxsel
;
2359 decoder_command(zr
, DECODER_SET_INPUT
, &input
);
2360 decoder_command(zr
, DECODER_SET_NORM
, &zr
->norm
);
2361 gstat_unlock_and_return
:
2362 mutex_unlock(&zr
->resource_lock
);
2366 (status
& DECODER_STATUS_GOOD
) ? 1 : 0;
2367 if (status
& DECODER_STATUS_NTSC
)
2368 bstat
->norm
= VIDEO_MODE_NTSC
;
2369 else if (status
& DECODER_STATUS_SECAM
)
2370 bstat
->norm
= VIDEO_MODE_SECAM
;
2372 bstat
->norm
= VIDEO_MODE_PAL
;
2375 (status
& DECODER_STATUS_COLOR
) ? 1 : 0;
2386 static int zoran_querycap(struct file
*file
, void *__fh
, struct v4l2_capability
*cap
)
2388 struct zoran_fh
*fh
= __fh
;
2389 struct zoran
*zr
= fh
->zr
;
2391 memset(cap
, 0, sizeof(*cap
));
2392 strncpy(cap
->card
, ZR_DEVNAME(zr
), sizeof(cap
->card
)-1);
2393 strncpy(cap
->driver
, "zoran", sizeof(cap
->driver
)-1);
2394 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "PCI:%s",
2395 pci_name(zr
->pci_dev
));
2397 KERNEL_VERSION(MAJOR_VERSION
, MINOR_VERSION
,
2399 cap
->capabilities
= ZORAN_V4L2_VID_FLAGS
;
2404 static int zoran_enum_fmt(struct zoran
*zr
, struct v4l2_fmtdesc
*fmt
, int flag
)
2408 for (i
= 0; i
< NUM_FORMATS
; i
++) {
2409 if (zoran_formats
[i
].flags
& flag
)
2411 if (num
== fmt
->index
)
2414 if (fmt
->index
< 0 /* late, but not too late */ || i
== NUM_FORMATS
)
2417 strncpy(fmt
->description
, zoran_formats
[i
].name
, sizeof(fmt
->description
)-1);
2418 fmt
->pixelformat
= zoran_formats
[i
].fourcc
;
2419 if (zoran_formats
[i
].flags
& ZORAN_FORMAT_COMPRESSED
)
2420 fmt
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
2424 static int zoran_enum_fmt_vid_cap(struct file
*file
, void *__fh
,
2425 struct v4l2_fmtdesc
*f
)
2427 struct zoran_fh
*fh
= __fh
;
2428 struct zoran
*zr
= fh
->zr
;
2430 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_CAPTURE
);
2433 static int zoran_enum_fmt_vid_out(struct file
*file
, void *__fh
,
2434 struct v4l2_fmtdesc
*f
)
2436 struct zoran_fh
*fh
= __fh
;
2437 struct zoran
*zr
= fh
->zr
;
2439 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_PLAYBACK
);
2442 static int zoran_enum_fmt_vid_overlay(struct file
*file
, void *__fh
,
2443 struct v4l2_fmtdesc
*f
)
2445 struct zoran_fh
*fh
= __fh
;
2446 struct zoran
*zr
= fh
->zr
;
2448 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_OVERLAY
);
2451 static int zoran_g_fmt_vid_out(struct file
*file
, void *__fh
,
2452 struct v4l2_format
*fmt
)
2454 struct zoran_fh
*fh
= __fh
;
2455 struct zoran
*zr
= fh
->zr
;
2457 mutex_lock(&zr
->resource_lock
);
2459 fmt
->fmt
.pix
.width
= fh
->jpg_settings
.img_width
/ fh
->jpg_settings
.HorDcm
;
2460 fmt
->fmt
.pix
.height
= fh
->jpg_settings
.img_height
/
2461 (fh
->jpg_settings
.VerDcm
* fh
->jpg_settings
.TmpDcm
);
2462 fmt
->fmt
.pix
.sizeimage
= zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
2463 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MJPEG
;
2464 if (fh
->jpg_settings
.TmpDcm
== 1)
2465 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2466 V4L2_FIELD_SEQ_BT
: V4L2_FIELD_SEQ_BT
);
2468 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2469 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
2470 fmt
->fmt
.pix
.bytesperline
= 0;
2471 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2473 mutex_unlock(&zr
->resource_lock
);
2477 static int zoran_g_fmt_vid_cap(struct file
*file
, void *__fh
,
2478 struct v4l2_format
*fmt
)
2480 struct zoran_fh
*fh
= __fh
;
2481 struct zoran
*zr
= fh
->zr
;
2483 if (fh
->map_mode
!= ZORAN_MAP_MODE_RAW
)
2484 return zoran_g_fmt_vid_out(file
, fh
, fmt
);
2486 mutex_lock(&zr
->resource_lock
);
2487 fmt
->fmt
.pix
.width
= fh
->v4l_settings
.width
;
2488 fmt
->fmt
.pix
.height
= fh
->v4l_settings
.height
;
2489 fmt
->fmt
.pix
.sizeimage
= fh
->v4l_settings
.bytesperline
*
2490 fh
->v4l_settings
.height
;
2491 fmt
->fmt
.pix
.pixelformat
= fh
->v4l_settings
.format
->fourcc
;
2492 fmt
->fmt
.pix
.colorspace
= fh
->v4l_settings
.format
->colorspace
;
2493 fmt
->fmt
.pix
.bytesperline
= fh
->v4l_settings
.bytesperline
;
2494 if (BUZ_MAX_HEIGHT
< (fh
->v4l_settings
.height
* 2))
2495 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
2497 fmt
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
2498 mutex_unlock(&zr
->resource_lock
);
2502 static int zoran_g_fmt_vid_overlay(struct file
*file
, void *__fh
,
2503 struct v4l2_format
*fmt
)
2505 struct zoran_fh
*fh
= __fh
;
2506 struct zoran
*zr
= fh
->zr
;
2508 mutex_lock(&zr
->resource_lock
);
2510 fmt
->fmt
.win
.w
.left
= fh
->overlay_settings
.x
;
2511 fmt
->fmt
.win
.w
.top
= fh
->overlay_settings
.y
;
2512 fmt
->fmt
.win
.w
.width
= fh
->overlay_settings
.width
;
2513 fmt
->fmt
.win
.w
.height
= fh
->overlay_settings
.height
;
2514 if (fh
->overlay_settings
.width
* 2 > BUZ_MAX_HEIGHT
)
2515 fmt
->fmt
.win
.field
= V4L2_FIELD_INTERLACED
;
2517 fmt
->fmt
.win
.field
= V4L2_FIELD_TOP
;
2519 mutex_unlock(&zr
->resource_lock
);
2523 static int zoran_try_fmt_vid_overlay(struct file
*file
, void *__fh
,
2524 struct v4l2_format
*fmt
)
2526 struct zoran_fh
*fh
= __fh
;
2527 struct zoran
*zr
= fh
->zr
;
2529 mutex_lock(&zr
->resource_lock
);
2531 if (fmt
->fmt
.win
.w
.width
> BUZ_MAX_WIDTH
)
2532 fmt
->fmt
.win
.w
.width
= BUZ_MAX_WIDTH
;
2533 if (fmt
->fmt
.win
.w
.width
< BUZ_MIN_WIDTH
)
2534 fmt
->fmt
.win
.w
.width
= BUZ_MIN_WIDTH
;
2535 if (fmt
->fmt
.win
.w
.height
> BUZ_MAX_HEIGHT
)
2536 fmt
->fmt
.win
.w
.height
= BUZ_MAX_HEIGHT
;
2537 if (fmt
->fmt
.win
.w
.height
< BUZ_MIN_HEIGHT
)
2538 fmt
->fmt
.win
.w
.height
= BUZ_MIN_HEIGHT
;
2540 mutex_unlock(&zr
->resource_lock
);
2544 static int zoran_try_fmt_vid_out(struct file
*file
, void *__fh
,
2545 struct v4l2_format
*fmt
)
2547 struct zoran_fh
*fh
= __fh
;
2548 struct zoran
*zr
= fh
->zr
;
2549 struct zoran_jpg_settings settings
;
2552 if (fmt
->fmt
.pix
.bytesperline
> 0)
2555 if (fmt
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
)
2558 mutex_lock(&zr
->resource_lock
);
2559 settings
= fh
->jpg_settings
;
2561 /* we actually need to set 'real' parameters now */
2562 if ((fmt
->fmt
.pix
.height
* 2) > BUZ_MAX_HEIGHT
)
2563 settings
.TmpDcm
= 1;
2565 settings
.TmpDcm
= 2;
2566 settings
.decimation
= 0;
2567 if (fmt
->fmt
.pix
.height
<= fh
->jpg_settings
.img_height
/ 2)
2568 settings
.VerDcm
= 2;
2570 settings
.VerDcm
= 1;
2571 if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 4)
2572 settings
.HorDcm
= 4;
2573 else if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 2)
2574 settings
.HorDcm
= 2;
2576 settings
.HorDcm
= 1;
2577 if (settings
.TmpDcm
== 1)
2578 settings
.field_per_buff
= 2;
2580 settings
.field_per_buff
= 1;
2583 res
= zoran_check_jpg_settings(zr
, &settings
);
2585 goto tryfmt_unlock_and_return
;
2587 /* tell the user what we actually did */
2588 fmt
->fmt
.pix
.width
= settings
.img_width
/ settings
.HorDcm
;
2589 fmt
->fmt
.pix
.height
= settings
.img_height
* 2 /
2590 (settings
.TmpDcm
* settings
.VerDcm
);
2591 if (settings
.TmpDcm
== 1)
2592 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2593 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
);
2595 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2596 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
2598 fmt
->fmt
.pix
.sizeimage
= zoran_v4l2_calc_bufsize(&settings
);
2599 tryfmt_unlock_and_return
:
2600 mutex_unlock(&zr
->resource_lock
);
2604 static int zoran_try_fmt_vid_cap(struct file
*file
, void *__fh
,
2605 struct v4l2_format
*fmt
)
2607 struct zoran_fh
*fh
= __fh
;
2608 struct zoran
*zr
= fh
->zr
;
2611 if (fmt
->fmt
.pix
.bytesperline
> 0)
2614 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_MJPEG
)
2615 return zoran_try_fmt_vid_out(file
, fh
, fmt
);
2617 mutex_lock(&zr
->resource_lock
);
2619 for (i
= 0; i
< NUM_FORMATS
; i
++)
2620 if (zoran_formats
[i
].fourcc
== fmt
->fmt
.pix
.pixelformat
)
2623 if (i
== NUM_FORMATS
) {
2624 mutex_unlock(&zr
->resource_lock
);
2628 if (fmt
->fmt
.pix
.width
> BUZ_MAX_WIDTH
)
2629 fmt
->fmt
.pix
.width
= BUZ_MAX_WIDTH
;
2630 if (fmt
->fmt
.pix
.width
< BUZ_MIN_WIDTH
)
2631 fmt
->fmt
.pix
.width
= BUZ_MIN_WIDTH
;
2632 if (fmt
->fmt
.pix
.height
> BUZ_MAX_HEIGHT
)
2633 fmt
->fmt
.pix
.height
= BUZ_MAX_HEIGHT
;
2634 if (fmt
->fmt
.pix
.height
< BUZ_MIN_HEIGHT
)
2635 fmt
->fmt
.pix
.height
= BUZ_MIN_HEIGHT
;
2636 mutex_unlock(&zr
->resource_lock
);
2641 static int zoran_s_fmt_vid_overlay(struct file
*file
, void *__fh
,
2642 struct v4l2_format
*fmt
)
2644 struct zoran_fh
*fh
= __fh
;
2645 struct zoran
*zr
= fh
->zr
;
2648 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2649 fmt
->fmt
.win
.w
.left
, fmt
->fmt
.win
.w
.top
,
2650 fmt
->fmt
.win
.w
.width
,
2651 fmt
->fmt
.win
.w
.height
,
2652 fmt
->fmt
.win
.clipcount
,
2653 fmt
->fmt
.win
.bitmap
);
2654 mutex_lock(&zr
->resource_lock
);
2655 res
= setup_window(file
, fmt
->fmt
.win
.w
.left
,
2657 fmt
->fmt
.win
.w
.width
,
2658 fmt
->fmt
.win
.w
.height
,
2659 (struct video_clip __user
*)
2661 fmt
->fmt
.win
.clipcount
,
2662 fmt
->fmt
.win
.bitmap
);
2663 mutex_unlock(&zr
->resource_lock
);
2667 static int zoran_s_fmt_vid_out(struct file
*file
, void *__fh
,
2668 struct v4l2_format
*fmt
)
2670 struct zoran_fh
*fh
= __fh
;
2671 struct zoran
*zr
= fh
->zr
;
2672 __le32 printformat
= __cpu_to_le32(fmt
->fmt
.pix
.pixelformat
);
2673 struct zoran_jpg_settings settings
;
2676 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2677 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
,
2678 fmt
->fmt
.pix
.pixelformat
,
2679 (char *) &printformat
);
2680 if (fmt
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
)
2683 mutex_lock(&zr
->resource_lock
);
2685 settings
= fh
->jpg_settings
;
2687 if (fh
->v4l_buffers
.allocated
|| fh
->jpg_buffers
.allocated
) {
2688 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - cannot change capture mode\n",
2691 goto sfmtjpg_unlock_and_return
;
2694 /* we actually need to set 'real' parameters now */
2695 if ((fmt
->fmt
.pix
.height
* 2) > BUZ_MAX_HEIGHT
)
2696 settings
.TmpDcm
= 1;
2698 settings
.TmpDcm
= 2;
2699 settings
.decimation
= 0;
2700 if (fmt
->fmt
.pix
.height
<= fh
->jpg_settings
.img_height
/ 2)
2701 settings
.VerDcm
= 2;
2703 settings
.VerDcm
= 1;
2704 if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 4)
2705 settings
.HorDcm
= 4;
2706 else if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 2)
2707 settings
.HorDcm
= 2;
2709 settings
.HorDcm
= 1;
2710 if (settings
.TmpDcm
== 1)
2711 settings
.field_per_buff
= 2;
2713 settings
.field_per_buff
= 1;
2716 res
= zoran_check_jpg_settings(zr
, &settings
);
2718 goto sfmtjpg_unlock_and_return
;
2720 /* it's ok, so set them */
2721 fh
->jpg_settings
= settings
;
2723 /* tell the user what we actually did */
2724 fmt
->fmt
.pix
.width
= settings
.img_width
/ settings
.HorDcm
;
2725 fmt
->fmt
.pix
.height
= settings
.img_height
* 2 /
2726 (settings
.TmpDcm
* settings
.VerDcm
);
2727 if (settings
.TmpDcm
== 1)
2728 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2729 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
);
2731 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
2732 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
2733 fh
->jpg_buffers
.buffer_size
= zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
2734 fmt
->fmt
.pix
.bytesperline
= 0;
2735 fmt
->fmt
.pix
.sizeimage
= fh
->jpg_buffers
.buffer_size
;
2736 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2738 /* we hereby abuse this variable to show that
2739 * we're gonna do mjpeg capture */
2740 fh
->map_mode
= (fmt
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) ?
2741 ZORAN_MAP_MODE_JPG_REC
: ZORAN_MAP_MODE_JPG_PLAY
;
2742 sfmtjpg_unlock_and_return
:
2743 mutex_unlock(&zr
->resource_lock
);
2747 static int zoran_s_fmt_vid_cap(struct file
*file
, void *__fh
,
2748 struct v4l2_format
*fmt
)
2750 struct zoran_fh
*fh
= __fh
;
2751 struct zoran
*zr
= fh
->zr
;
2755 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_MJPEG
)
2756 return zoran_s_fmt_vid_out(file
, fh
, fmt
);
2758 for (i
= 0; i
< NUM_FORMATS
; i
++)
2759 if (fmt
->fmt
.pix
.pixelformat
== zoran_formats
[i
].fourcc
)
2761 if (i
== NUM_FORMATS
) {
2762 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2763 ZR_DEVNAME(zr
), fmt
->fmt
.pix
.pixelformat
);
2766 mutex_lock(&zr
->resource_lock
);
2767 if (fh
->jpg_buffers
.allocated
||
2768 (fh
->v4l_buffers
.allocated
&& fh
->v4l_buffers
.active
!= ZORAN_FREE
)) {
2769 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - cannot change capture mode\n",
2772 goto sfmtv4l_unlock_and_return
;
2774 if (fmt
->fmt
.pix
.height
> BUZ_MAX_HEIGHT
)
2775 fmt
->fmt
.pix
.height
= BUZ_MAX_HEIGHT
;
2776 if (fmt
->fmt
.pix
.width
> BUZ_MAX_WIDTH
)
2777 fmt
->fmt
.pix
.width
= BUZ_MAX_WIDTH
;
2779 res
= zoran_v4l_set_format(file
, fmt
->fmt
.pix
.width
,
2780 fmt
->fmt
.pix
.height
, &zoran_formats
[i
]);
2782 goto sfmtv4l_unlock_and_return
;
2784 /* tell the user the
2785 * results/missing stuff */
2786 fmt
->fmt
.pix
.bytesperline
= fh
->v4l_settings
.bytesperline
;
2787 fmt
->fmt
.pix
.sizeimage
= fh
->v4l_settings
.height
* fh
->v4l_settings
.bytesperline
;
2788 fmt
->fmt
.pix
.colorspace
= fh
->v4l_settings
.format
->colorspace
;
2789 if (BUZ_MAX_HEIGHT
< (fh
->v4l_settings
.height
* 2))
2790 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
2792 fmt
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
2794 fh
->map_mode
= ZORAN_MAP_MODE_RAW
;
2795 sfmtv4l_unlock_and_return
:
2796 mutex_unlock(&zr
->resource_lock
);
2800 static int zoran_g_fbuf(struct file
*file
, void *__fh
,
2801 struct v4l2_framebuffer
*fb
)
2803 struct zoran_fh
*fh
= __fh
;
2804 struct zoran
*zr
= fh
->zr
;
2806 memset(fb
, 0, sizeof(*fb
));
2807 mutex_lock(&zr
->resource_lock
);
2808 fb
->base
= zr
->buffer
.base
;
2809 fb
->fmt
.width
= zr
->buffer
.width
;
2810 fb
->fmt
.height
= zr
->buffer
.height
;
2811 if (zr
->overlay_settings
.format
)
2812 fb
->fmt
.pixelformat
= fh
->overlay_settings
.format
->fourcc
;
2813 fb
->fmt
.bytesperline
= zr
->buffer
.bytesperline
;
2814 mutex_unlock(&zr
->resource_lock
);
2815 fb
->fmt
.colorspace
= V4L2_COLORSPACE_SRGB
;
2816 fb
->fmt
.field
= V4L2_FIELD_INTERLACED
;
2817 fb
->flags
= V4L2_FBUF_FLAG_OVERLAY
;
2818 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2823 static int zoran_s_fbuf(struct file
*file
, void *__fh
,
2824 struct v4l2_framebuffer
*fb
)
2826 struct zoran_fh
*fh
= __fh
;
2827 struct zoran
*zr
= fh
->zr
;
2829 __le32 printformat
= __cpu_to_le32(fb
->fmt
.pixelformat
);
2831 for (i
= 0; i
< NUM_FORMATS
; i
++)
2832 if (zoran_formats
[i
].fourcc
== fb
->fmt
.pixelformat
)
2834 if (i
== NUM_FORMATS
) {
2835 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2836 ZR_DEVNAME(zr
), fb
->fmt
.pixelformat
,
2837 (char *)&printformat
);
2841 mutex_lock(&zr
->resource_lock
);
2842 res
= setup_fbuffer(file
, fb
->base
, &zoran_formats
[i
],
2843 fb
->fmt
.width
, fb
->fmt
.height
,
2844 fb
->fmt
.bytesperline
);
2845 mutex_unlock(&zr
->resource_lock
);
2850 static int zoran_overlay(struct file
*file
, void *__fh
, unsigned int on
)
2852 struct zoran_fh
*fh
= __fh
;
2853 struct zoran
*zr
= fh
->zr
;
2856 mutex_lock(&zr
->resource_lock
);
2857 res
= setup_overlay(file
, on
);
2858 mutex_unlock(&zr
->resource_lock
);
2863 static int zoran_reqbufs(struct file
*file
, void *__fh
, struct v4l2_requestbuffers
*req
)
2865 struct zoran_fh
*fh
= __fh
;
2866 struct zoran
*zr
= fh
->zr
;
2869 if (req
->memory
!= V4L2_MEMORY_MMAP
) {
2872 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2873 ZR_DEVNAME(zr
), req
->memory
);
2877 mutex_lock(&zr
->resource_lock
);
2879 if (fh
->v4l_buffers
.allocated
|| fh
->jpg_buffers
.allocated
) {
2882 "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
2885 goto v4l2reqbuf_unlock_and_return
;
2888 if (fh
->map_mode
== ZORAN_MAP_MODE_RAW
&&
2889 req
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2891 /* control user input */
2894 if (req
->count
> v4l_nbufs
)
2895 req
->count
= v4l_nbufs
;
2896 fh
->v4l_buffers
.num_buffers
= req
->count
;
2898 if (v4l_fbuffer_alloc(file
)) {
2900 goto v4l2reqbuf_unlock_and_return
;
2903 /* The next mmap will map the V4L buffers */
2904 fh
->map_mode
= ZORAN_MAP_MODE_RAW
;
2906 } else if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
||
2907 fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
) {
2909 /* we need to calculate size ourselves now */
2912 if (req
->count
> jpg_nbufs
)
2913 req
->count
= jpg_nbufs
;
2914 fh
->jpg_buffers
.num_buffers
= req
->count
;
2915 fh
->jpg_buffers
.buffer_size
=
2916 zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
2918 if (jpg_fbuffer_alloc(file
)) {
2920 goto v4l2reqbuf_unlock_and_return
;
2923 /* The next mmap will map the MJPEG buffers */
2924 if (req
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2925 fh
->map_mode
= ZORAN_MAP_MODE_JPG_REC
;
2927 fh
->map_mode
= ZORAN_MAP_MODE_JPG_PLAY
;
2932 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2933 ZR_DEVNAME(zr
), req
->type
);
2935 goto v4l2reqbuf_unlock_and_return
;
2937 v4l2reqbuf_unlock_and_return
:
2938 mutex_unlock(&zr
->resource_lock
);
2943 static int zoran_querybuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
2945 struct zoran_fh
*fh
= __fh
;
2946 struct zoran
*zr
= fh
->zr
;
2947 __u32 type
= buf
->type
;
2948 int index
= buf
->index
, res
;
2950 memset(buf
, 0, sizeof(*buf
));
2954 mutex_lock(&zr
->resource_lock
);
2955 res
= zoran_v4l2_buffer_status(file
, buf
, buf
->index
);
2956 mutex_unlock(&zr
->resource_lock
);
2961 static int zoran_qbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
2963 struct zoran_fh
*fh
= __fh
;
2964 struct zoran
*zr
= fh
->zr
;
2965 int res
= 0, codec_mode
, buf_type
;
2967 mutex_lock(&zr
->resource_lock
);
2969 switch (fh
->map_mode
) {
2970 case ZORAN_MAP_MODE_RAW
:
2971 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2973 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2974 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
2976 goto qbuf_unlock_and_return
;
2979 res
= zoran_v4l_queue_frame(file
, buf
->index
);
2981 goto qbuf_unlock_and_return
;
2982 if (!zr
->v4l_memgrab_active
&&
2983 fh
->v4l_buffers
.active
== ZORAN_LOCKED
)
2984 zr36057_set_memgrab(zr
, 1);
2987 case ZORAN_MAP_MODE_JPG_REC
:
2988 case ZORAN_MAP_MODE_JPG_PLAY
:
2989 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
) {
2990 buf_type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2991 codec_mode
= BUZ_MODE_MOTION_DECOMPRESS
;
2993 buf_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2994 codec_mode
= BUZ_MODE_MOTION_COMPRESS
;
2997 if (buf
->type
!= buf_type
) {
2999 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3000 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
3002 goto qbuf_unlock_and_return
;
3005 res
= zoran_jpg_queue_frame(file
, buf
->index
,
3008 goto qbuf_unlock_and_return
;
3009 if (zr
->codec_mode
== BUZ_MODE_IDLE
&&
3010 fh
->jpg_buffers
.active
== ZORAN_LOCKED
) {
3011 zr36057_enable_jpg(zr
, codec_mode
);
3017 "%s: VIDIOC_QBUF - unsupported type %d\n",
3018 ZR_DEVNAME(zr
), buf
->type
);
3022 qbuf_unlock_and_return
:
3023 mutex_unlock(&zr
->resource_lock
);
3028 static int zoran_dqbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
3030 struct zoran_fh
*fh
= __fh
;
3031 struct zoran
*zr
= fh
->zr
;
3032 int res
= 0, buf_type
, num
= -1; /* compiler borks here (?) */
3034 mutex_lock(&zr
->resource_lock
);
3036 switch (fh
->map_mode
) {
3037 case ZORAN_MAP_MODE_RAW
:
3038 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
3040 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3041 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
3043 goto dqbuf_unlock_and_return
;
3046 num
= zr
->v4l_pend
[zr
->v4l_sync_tail
& V4L_MASK_FRAME
];
3047 if (file
->f_flags
& O_NONBLOCK
&&
3048 zr
->v4l_buffers
.buffer
[num
].state
!= BUZ_STATE_DONE
) {
3050 goto dqbuf_unlock_and_return
;
3052 res
= v4l_sync(file
, num
);
3054 goto dqbuf_unlock_and_return
;
3055 zr
->v4l_sync_tail
++;
3056 res
= zoran_v4l2_buffer_status(file
, buf
, num
);
3059 case ZORAN_MAP_MODE_JPG_REC
:
3060 case ZORAN_MAP_MODE_JPG_PLAY
:
3062 struct zoran_sync bs
;
3064 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
)
3065 buf_type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
3067 buf_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3069 if (buf
->type
!= buf_type
) {
3071 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
3072 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
3074 goto dqbuf_unlock_and_return
;
3077 num
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
3079 if (file
->f_flags
& O_NONBLOCK
&&
3080 zr
->jpg_buffers
.buffer
[num
].state
!= BUZ_STATE_DONE
) {
3082 goto dqbuf_unlock_and_return
;
3084 res
= jpg_sync(file
, &bs
);
3086 goto dqbuf_unlock_and_return
;
3087 res
= zoran_v4l2_buffer_status(file
, buf
, bs
.frame
);
3093 "%s: VIDIOC_DQBUF - unsupported type %d\n",
3094 ZR_DEVNAME(zr
), buf
->type
);
3098 dqbuf_unlock_and_return
:
3099 mutex_unlock(&zr
->resource_lock
);
3104 static int zoran_streamon(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
3106 struct zoran_fh
*fh
= __fh
;
3107 struct zoran
*zr
= fh
->zr
;
3110 mutex_lock(&zr
->resource_lock
);
3112 switch (fh
->map_mode
) {
3113 case ZORAN_MAP_MODE_RAW
: /* raw capture */
3114 if (zr
->v4l_buffers
.active
!= ZORAN_ACTIVE
||
3115 fh
->v4l_buffers
.active
!= ZORAN_ACTIVE
) {
3117 goto strmon_unlock_and_return
;
3120 zr
->v4l_buffers
.active
= fh
->v4l_buffers
.active
= ZORAN_LOCKED
;
3121 zr
->v4l_settings
= fh
->v4l_settings
;
3123 zr
->v4l_sync_tail
= zr
->v4l_pend_tail
;
3124 if (!zr
->v4l_memgrab_active
&&
3125 zr
->v4l_pend_head
!= zr
->v4l_pend_tail
) {
3126 zr36057_set_memgrab(zr
, 1);
3130 case ZORAN_MAP_MODE_JPG_REC
:
3131 case ZORAN_MAP_MODE_JPG_PLAY
:
3132 /* what is the codec mode right now? */
3133 if (zr
->jpg_buffers
.active
!= ZORAN_ACTIVE
||
3134 fh
->jpg_buffers
.active
!= ZORAN_ACTIVE
) {
3136 goto strmon_unlock_and_return
;
3139 zr
->jpg_buffers
.active
= fh
->jpg_buffers
.active
= ZORAN_LOCKED
;
3141 if (zr
->jpg_que_head
!= zr
->jpg_que_tail
) {
3142 /* Start the jpeg codec when the first frame is queued */
3150 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
3151 ZR_DEVNAME(zr
), fh
->map_mode
);
3155 strmon_unlock_and_return
:
3156 mutex_unlock(&zr
->resource_lock
);
3161 static int zoran_streamoff(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
3163 struct zoran_fh
*fh
= __fh
;
3164 struct zoran
*zr
= fh
->zr
;
3167 mutex_lock(&zr
->resource_lock
);
3169 switch (fh
->map_mode
) {
3170 case ZORAN_MAP_MODE_RAW
: /* raw capture */
3171 if (fh
->v4l_buffers
.active
== ZORAN_FREE
&&
3172 zr
->v4l_buffers
.active
!= ZORAN_FREE
) {
3173 res
= -EPERM
; /* stay off other's settings! */
3174 goto strmoff_unlock_and_return
;
3176 if (zr
->v4l_buffers
.active
== ZORAN_FREE
)
3177 goto strmoff_unlock_and_return
;
3179 /* unload capture */
3180 if (zr
->v4l_memgrab_active
) {
3181 unsigned long flags
;
3183 spin_lock_irqsave(&zr
->spinlock
, flags
);
3184 zr36057_set_memgrab(zr
, 0);
3185 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
3188 for (i
= 0; i
< fh
->v4l_buffers
.num_buffers
; i
++)
3189 zr
->v4l_buffers
.buffer
[i
].state
= BUZ_STATE_USER
;
3190 fh
->v4l_buffers
= zr
->v4l_buffers
;
3192 zr
->v4l_buffers
.active
= fh
->v4l_buffers
.active
= ZORAN_FREE
;
3194 zr
->v4l_grab_seq
= 0;
3195 zr
->v4l_pend_head
= zr
->v4l_pend_tail
= 0;
3196 zr
->v4l_sync_tail
= 0;
3200 case ZORAN_MAP_MODE_JPG_REC
:
3201 case ZORAN_MAP_MODE_JPG_PLAY
:
3202 if (fh
->jpg_buffers
.active
== ZORAN_FREE
&&
3203 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
3204 res
= -EPERM
; /* stay off other's settings! */
3205 goto strmoff_unlock_and_return
;
3207 if (zr
->jpg_buffers
.active
== ZORAN_FREE
)
3208 goto strmoff_unlock_and_return
;
3210 res
= jpg_qbuf(file
, -1,
3211 (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
) ?
3212 BUZ_MODE_MOTION_COMPRESS
:
3213 BUZ_MODE_MOTION_DECOMPRESS
);
3215 goto strmoff_unlock_and_return
;
3219 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
3220 ZR_DEVNAME(zr
), fh
->map_mode
);
3224 strmoff_unlock_and_return
:
3225 mutex_unlock(&zr
->resource_lock
);
3230 static int zoran_queryctrl(struct file
*file
, void *__fh
,
3231 struct v4l2_queryctrl
*ctrl
)
3233 /* we only support hue/saturation/contrast/brightness */
3234 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
3235 ctrl
->id
> V4L2_CID_HUE
)
3239 memset(ctrl
, 0, sizeof(*ctrl
));
3244 case V4L2_CID_BRIGHTNESS
:
3245 strncpy(ctrl
->name
, "Brightness", sizeof(ctrl
->name
)-1);
3247 case V4L2_CID_CONTRAST
:
3248 strncpy(ctrl
->name
, "Contrast", sizeof(ctrl
->name
)-1);
3250 case V4L2_CID_SATURATION
:
3251 strncpy(ctrl
->name
, "Saturation", sizeof(ctrl
->name
)-1);
3254 strncpy(ctrl
->name
, "Hue", sizeof(ctrl
->name
)-1);
3259 ctrl
->maximum
= 65535;
3261 ctrl
->default_value
= 32768;
3262 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
3267 static int zoran_g_ctrl(struct file
*file
, void *__fh
, struct v4l2_control
*ctrl
)
3269 struct zoran_fh
*fh
= __fh
;
3270 struct zoran
*zr
= fh
->zr
;
3272 /* we only support hue/saturation/contrast/brightness */
3273 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
3274 ctrl
->id
> V4L2_CID_HUE
)
3277 mutex_lock(&zr
->resource_lock
);
3279 case V4L2_CID_BRIGHTNESS
:
3280 ctrl
->value
= zr
->brightness
;
3282 case V4L2_CID_CONTRAST
:
3283 ctrl
->value
= zr
->contrast
;
3285 case V4L2_CID_SATURATION
:
3286 ctrl
->value
= zr
->saturation
;
3289 ctrl
->value
= zr
->hue
;
3292 mutex_unlock(&zr
->resource_lock
);
3297 static int zoran_s_ctrl(struct file
*file
, void *__fh
, struct v4l2_control
*ctrl
)
3299 struct zoran_fh
*fh
= __fh
;
3300 struct zoran
*zr
= fh
->zr
;
3301 struct video_picture pict
;
3303 /* we only support hue/saturation/contrast/brightness */
3304 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
3305 ctrl
->id
> V4L2_CID_HUE
)
3308 if (ctrl
->value
< 0 || ctrl
->value
> 65535) {
3310 "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
3311 ZR_DEVNAME(zr
), ctrl
->value
, ctrl
->id
);
3315 mutex_lock(&zr
->resource_lock
);
3317 case V4L2_CID_BRIGHTNESS
:
3318 zr
->brightness
= ctrl
->value
;
3320 case V4L2_CID_CONTRAST
:
3321 zr
->contrast
= ctrl
->value
;
3323 case V4L2_CID_SATURATION
:
3324 zr
->saturation
= ctrl
->value
;
3327 zr
->hue
= ctrl
->value
;
3330 pict
.brightness
= zr
->brightness
;
3331 pict
.contrast
= zr
->contrast
;
3332 pict
.colour
= zr
->saturation
;
3335 decoder_command(zr
, DECODER_SET_PICTURE
, &pict
);
3337 mutex_unlock(&zr
->resource_lock
);
3342 static int zoran_g_std(struct file
*file
, void *__fh
, v4l2_std_id
*std
)
3344 struct zoran_fh
*fh
= __fh
;
3345 struct zoran
*zr
= fh
->zr
;
3348 mutex_lock(&zr
->resource_lock
);
3350 mutex_unlock(&zr
->resource_lock
);
3353 case VIDEO_MODE_PAL
:
3354 *std
= V4L2_STD_PAL
;
3356 case VIDEO_MODE_NTSC
:
3357 *std
= V4L2_STD_NTSC
;
3359 case VIDEO_MODE_SECAM
:
3360 *std
= V4L2_STD_SECAM
;
3367 static int zoran_s_std(struct file
*file
, void *__fh
, v4l2_std_id
*std
)
3369 struct zoran_fh
*fh
= __fh
;
3370 struct zoran
*zr
= fh
->zr
;
3371 int norm
= -1, res
= 0;
3373 if ((*std
& V4L2_STD_PAL
) && !(*std
& ~V4L2_STD_PAL
))
3374 norm
= VIDEO_MODE_PAL
;
3375 else if ((*std
& V4L2_STD_NTSC
) && !(*std
& ~V4L2_STD_NTSC
))
3376 norm
= VIDEO_MODE_NTSC
;
3377 else if ((*std
& V4L2_STD_SECAM
) && !(*std
& ~V4L2_STD_SECAM
))
3378 norm
= VIDEO_MODE_SECAM
;
3379 else if (*std
== V4L2_STD_ALL
)
3380 norm
= VIDEO_MODE_AUTO
;
3383 "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
3384 ZR_DEVNAME(zr
), (unsigned long long)*std
);
3388 mutex_lock(&zr
->resource_lock
);
3389 res
= zoran_set_norm(zr
, norm
);
3391 goto sstd_unlock_and_return
;
3393 res
= wait_grab_pending(zr
);
3394 sstd_unlock_and_return
:
3395 mutex_unlock(&zr
->resource_lock
);
3399 static int zoran_enum_input(struct file
*file
, void *__fh
,
3400 struct v4l2_input
*inp
)
3402 struct zoran_fh
*fh
= __fh
;
3403 struct zoran
*zr
= fh
->zr
;
3406 if (inp
->index
< 0 || inp
->index
>= zr
->card
.inputs
)
3409 int id
= inp
->index
;
3410 memset(inp
, 0, sizeof(*inp
));
3414 strncpy(inp
->name
, zr
->card
.input
[inp
->index
].name
,
3415 sizeof(inp
->name
) - 1);
3416 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
3417 inp
->std
= V4L2_STD_ALL
;
3419 /* Get status of video decoder */
3420 mutex_lock(&zr
->resource_lock
);
3421 decoder_command(zr
, DECODER_GET_STATUS
, &status
);
3422 mutex_unlock(&zr
->resource_lock
);
3424 if (!(status
& DECODER_STATUS_GOOD
)) {
3425 inp
->status
|= V4L2_IN_ST_NO_POWER
;
3426 inp
->status
|= V4L2_IN_ST_NO_SIGNAL
;
3428 if (!(status
& DECODER_STATUS_COLOR
))
3429 inp
->status
|= V4L2_IN_ST_NO_COLOR
;
3434 static int zoran_g_input(struct file
*file
, void *__fh
, unsigned int *input
)
3436 struct zoran_fh
*fh
= __fh
;
3437 struct zoran
*zr
= fh
->zr
;
3439 mutex_lock(&zr
->resource_lock
);
3441 mutex_unlock(&zr
->resource_lock
);
3446 static int zoran_s_input(struct file
*file
, void *__fh
, unsigned int input
)
3448 struct zoran_fh
*fh
= __fh
;
3449 struct zoran
*zr
= fh
->zr
;
3452 mutex_lock(&zr
->resource_lock
);
3453 res
= zoran_set_input(zr
, input
);
3455 goto sinput_unlock_and_return
;
3457 /* Make sure the changes come into effect */
3458 res
= wait_grab_pending(zr
);
3459 sinput_unlock_and_return
:
3460 mutex_unlock(&zr
->resource_lock
);
3464 static int zoran_enum_output(struct file
*file
, void *__fh
,
3465 struct v4l2_output
*outp
)
3467 if (outp
->index
!= 0)
3470 memset(outp
, 0, sizeof(*outp
));
3472 outp
->type
= V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY
;
3473 strncpy(outp
->name
, "Autodetect", sizeof(outp
->name
)-1);
3478 static int zoran_g_output(struct file
*file
, void *__fh
, unsigned int *output
)
3485 static int zoran_s_output(struct file
*file
, void *__fh
, unsigned int output
)
3493 /* cropping (sub-frame capture) */
3494 static int zoran_cropcap(struct file
*file
, void *__fh
,
3495 struct v4l2_cropcap
*cropcap
)
3497 struct zoran_fh
*fh
= __fh
;
3498 struct zoran
*zr
= fh
->zr
;
3499 int type
= cropcap
->type
, res
= 0;
3501 memset(cropcap
, 0, sizeof(*cropcap
));
3502 cropcap
->type
= type
;
3504 mutex_lock(&zr
->resource_lock
);
3506 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
3507 (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
3508 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
3510 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
3513 goto cropcap_unlock_and_return
;
3516 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
3517 cropcap
->bounds
.width
= BUZ_MAX_WIDTH
;
3518 cropcap
->bounds
.height
= BUZ_MAX_HEIGHT
;
3519 cropcap
->defrect
.top
= cropcap
->defrect
.left
= 0;
3520 cropcap
->defrect
.width
= BUZ_MIN_WIDTH
;
3521 cropcap
->defrect
.height
= BUZ_MIN_HEIGHT
;
3522 cropcap_unlock_and_return
:
3523 mutex_unlock(&zr
->resource_lock
);
3527 static int zoran_g_crop(struct file
*file
, void *__fh
, struct v4l2_crop
*crop
)
3529 struct zoran_fh
*fh
= __fh
;
3530 struct zoran
*zr
= fh
->zr
;
3531 int type
= crop
->type
, res
= 0;
3533 memset(crop
, 0, sizeof(*crop
));
3536 mutex_lock(&zr
->resource_lock
);
3538 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
3539 (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
3540 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
3543 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3546 goto gcrop_unlock_and_return
;
3549 crop
->c
.top
= fh
->jpg_settings
.img_y
;
3550 crop
->c
.left
= fh
->jpg_settings
.img_x
;
3551 crop
->c
.width
= fh
->jpg_settings
.img_width
;
3552 crop
->c
.height
= fh
->jpg_settings
.img_height
;
3554 gcrop_unlock_and_return
:
3555 mutex_unlock(&zr
->resource_lock
);
3560 static int zoran_s_crop(struct file
*file
, void *__fh
, struct v4l2_crop
*crop
)
3562 struct zoran_fh
*fh
= __fh
;
3563 struct zoran
*zr
= fh
->zr
;
3565 struct zoran_jpg_settings settings
;
3567 settings
= fh
->jpg_settings
;
3569 mutex_lock(&zr
->resource_lock
);
3571 if (fh
->jpg_buffers
.allocated
|| fh
->v4l_buffers
.allocated
) {
3573 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
3576 goto scrop_unlock_and_return
;
3579 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
3580 (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
3581 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
3583 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
3586 goto scrop_unlock_and_return
;
3589 /* move into a form that we understand */
3590 settings
.img_x
= crop
->c
.left
;
3591 settings
.img_y
= crop
->c
.top
;
3592 settings
.img_width
= crop
->c
.width
;
3593 settings
.img_height
= crop
->c
.height
;
3595 /* check validity */
3596 res
= zoran_check_jpg_settings(zr
, &settings
);
3598 goto scrop_unlock_and_return
;
3601 fh
->jpg_settings
= settings
;
3603 scrop_unlock_and_return
:
3604 mutex_unlock(&zr
->resource_lock
);
3608 static int zoran_g_jpegcomp(struct file
*file
, void *__fh
,
3609 struct v4l2_jpegcompression
*params
)
3611 struct zoran_fh
*fh
= __fh
;
3612 struct zoran
*zr
= fh
->zr
;
3613 memset(params
, 0, sizeof(*params
));
3615 mutex_lock(&zr
->resource_lock
);
3617 params
->quality
= fh
->jpg_settings
.jpg_comp
.quality
;
3618 params
->APPn
= fh
->jpg_settings
.jpg_comp
.APPn
;
3619 memcpy(params
->APP_data
,
3620 fh
->jpg_settings
.jpg_comp
.APP_data
,
3621 fh
->jpg_settings
.jpg_comp
.APP_len
);
3622 params
->APP_len
= fh
->jpg_settings
.jpg_comp
.APP_len
;
3623 memcpy(params
->COM_data
,
3624 fh
->jpg_settings
.jpg_comp
.COM_data
,
3625 fh
->jpg_settings
.jpg_comp
.COM_len
);
3626 params
->COM_len
= fh
->jpg_settings
.jpg_comp
.COM_len
;
3627 params
->jpeg_markers
=
3628 fh
->jpg_settings
.jpg_comp
.jpeg_markers
;
3630 mutex_unlock(&zr
->resource_lock
);
3635 static int zoran_s_jpegcomp(struct file
*file
, void *__fh
,
3636 struct v4l2_jpegcompression
*params
)
3638 struct zoran_fh
*fh
= __fh
;
3639 struct zoran
*zr
= fh
->zr
;
3641 struct zoran_jpg_settings settings
;
3643 settings
= fh
->jpg_settings
;
3645 settings
.jpg_comp
= *params
;
3647 mutex_lock(&zr
->resource_lock
);
3649 if (fh
->v4l_buffers
.active
!= ZORAN_FREE
||
3650 fh
->jpg_buffers
.active
!= ZORAN_FREE
) {
3651 dprintk(1, KERN_WARNING
3652 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3655 goto sjpegc_unlock_and_return
;
3658 res
= zoran_check_jpg_settings(zr
, &settings
);
3660 goto sjpegc_unlock_and_return
;
3661 if (!fh
->jpg_buffers
.allocated
)
3662 fh
->jpg_buffers
.buffer_size
=
3663 zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
3664 fh
->jpg_settings
.jpg_comp
= *params
= settings
.jpg_comp
;
3665 sjpegc_unlock_and_return
:
3666 mutex_unlock(&zr
->resource_lock
);
3672 zoran_poll (struct file
*file
,
3675 struct zoran_fh
*fh
= file
->private_data
;
3676 struct zoran
*zr
= fh
->zr
;
3678 unsigned long flags
;
3680 /* we should check whether buffers are ready to be synced on
3681 * (w/o waits - O_NONBLOCK) here
3682 * if ready for read (sync), return POLLIN|POLLRDNORM,
3683 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3684 * if error, return POLLERR,
3685 * if no buffers queued or so, return POLLNVAL
3688 mutex_lock(&zr
->resource_lock
);
3690 switch (fh
->map_mode
) {
3691 case ZORAN_MAP_MODE_RAW
:
3692 poll_wait(file
, &zr
->v4l_capq
, wait
);
3693 frame
= zr
->v4l_pend
[zr
->v4l_sync_tail
& V4L_MASK_FRAME
];
3695 spin_lock_irqsave(&zr
->spinlock
, flags
);
3698 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
3699 ZR_DEVNAME(zr
), __func__
,
3700 "FAL"[fh
->v4l_buffers
.active
], zr
->v4l_sync_tail
,
3701 "UPMD"[zr
->v4l_buffers
.buffer
[frame
].state
],
3702 zr
->v4l_pend_tail
, zr
->v4l_pend_head
);
3703 /* Process is the one capturing? */
3704 if (fh
->v4l_buffers
.active
!= ZORAN_FREE
&&
3705 /* Buffer ready to DQBUF? */
3706 zr
->v4l_buffers
.buffer
[frame
].state
== BUZ_STATE_DONE
)
3707 res
= POLLIN
| POLLRDNORM
;
3708 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
3712 case ZORAN_MAP_MODE_JPG_REC
:
3713 case ZORAN_MAP_MODE_JPG_PLAY
:
3714 poll_wait(file
, &zr
->jpg_capq
, wait
);
3715 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
3717 spin_lock_irqsave(&zr
->spinlock
, flags
);
3720 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
3721 ZR_DEVNAME(zr
), __func__
,
3722 "FAL"[fh
->jpg_buffers
.active
], zr
->jpg_que_tail
,
3723 "UPMD"[zr
->jpg_buffers
.buffer
[frame
].state
],
3724 zr
->jpg_que_head
, zr
->jpg_dma_tail
, zr
->jpg_dma_head
);
3725 if (fh
->jpg_buffers
.active
!= ZORAN_FREE
&&
3726 zr
->jpg_buffers
.buffer
[frame
].state
== BUZ_STATE_DONE
) {
3727 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
)
3728 res
= POLLIN
| POLLRDNORM
;
3730 res
= POLLOUT
| POLLWRNORM
;
3732 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
3739 "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
3740 ZR_DEVNAME(zr
), fh
->map_mode
);
3744 mutex_unlock(&zr
->resource_lock
);
3751 * This maps the buffers to user space.
3753 * Depending on the state of fh->map_mode
3754 * the V4L or the MJPEG buffers are mapped
3755 * per buffer or all together
3757 * Note that we need to connect to some
3758 * unmap signal event to unmap the de-allocate
3759 * the buffer accordingly (zoran_vm_close())
3763 zoran_vm_open (struct vm_area_struct
*vma
)
3765 struct zoran_mapping
*map
= vma
->vm_private_data
;
3771 zoran_vm_close (struct vm_area_struct
*vma
)
3773 struct zoran_mapping
*map
= vma
->vm_private_data
;
3774 struct file
*file
= map
->file
;
3775 struct zoran_fh
*fh
= file
->private_data
;
3776 struct zoran
*zr
= fh
->zr
;
3780 if (map
->count
== 0) {
3781 switch (fh
->map_mode
) {
3782 case ZORAN_MAP_MODE_JPG_REC
:
3783 case ZORAN_MAP_MODE_JPG_PLAY
:
3785 dprintk(3, KERN_INFO
"%s: munmap(MJPEG)\n",
3788 for (i
= 0; i
< fh
->jpg_buffers
.num_buffers
; i
++) {
3789 if (fh
->jpg_buffers
.buffer
[i
].map
== map
) {
3790 fh
->jpg_buffers
.buffer
[i
].map
=
3796 for (i
= 0; i
< fh
->jpg_buffers
.num_buffers
; i
++)
3797 if (fh
->jpg_buffers
.buffer
[i
].map
)
3799 if (i
== fh
->jpg_buffers
.num_buffers
) {
3800 mutex_lock(&zr
->resource_lock
);
3802 if (fh
->jpg_buffers
.active
!= ZORAN_FREE
) {
3803 jpg_qbuf(file
, -1, zr
->codec_mode
);
3804 zr
->jpg_buffers
.allocated
= 0;
3805 zr
->jpg_buffers
.active
=
3806 fh
->jpg_buffers
.active
=
3809 jpg_fbuffer_free(file
);
3810 mutex_unlock(&zr
->resource_lock
);
3815 case ZORAN_MAP_MODE_RAW
:
3817 dprintk(3, KERN_INFO
"%s: munmap(V4L)\n",
3820 for (i
= 0; i
< fh
->v4l_buffers
.num_buffers
; i
++) {
3821 if (fh
->v4l_buffers
.buffer
[i
].map
== map
) {
3823 fh
->v4l_buffers
.buffer
[i
].map
=
3829 for (i
= 0; i
< fh
->v4l_buffers
.num_buffers
; i
++)
3830 if (fh
->v4l_buffers
.buffer
[i
].map
)
3832 if (i
== fh
->v4l_buffers
.num_buffers
) {
3833 mutex_lock(&zr
->resource_lock
);
3835 if (fh
->v4l_buffers
.active
!= ZORAN_FREE
) {
3836 unsigned long flags
;
3838 spin_lock_irqsave(&zr
->spinlock
, flags
);
3839 zr36057_set_memgrab(zr
, 0);
3840 zr
->v4l_buffers
.allocated
= 0;
3841 zr
->v4l_buffers
.active
=
3842 fh
->v4l_buffers
.active
=
3844 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
3846 v4l_fbuffer_free(file
);
3847 mutex_unlock(&zr
->resource_lock
);
3854 "%s: munmap() - internal error - unknown map mode %d\n",
3855 ZR_DEVNAME(zr
), fh
->map_mode
);
3862 static struct vm_operations_struct zoran_vm_ops
= {
3863 .open
= zoran_vm_open
,
3864 .close
= zoran_vm_close
,
3868 zoran_mmap (struct file
*file
,
3869 struct vm_area_struct
*vma
)
3871 struct zoran_fh
*fh
= file
->private_data
;
3872 struct zoran
*zr
= fh
->zr
;
3873 unsigned long size
= (vma
->vm_end
- vma
->vm_start
);
3874 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
3876 unsigned long page
, start
= vma
->vm_start
, todo
, pos
, fraglen
;
3878 struct zoran_mapping
*map
;
3882 KERN_INFO
"%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3884 fh
->map_mode
== ZORAN_MAP_MODE_RAW
? "V4L" : "MJPEG",
3885 vma
->vm_start
, vma
->vm_end
, size
);
3887 if (!(vma
->vm_flags
& VM_SHARED
) || !(vma
->vm_flags
& VM_READ
) ||
3888 !(vma
->vm_flags
& VM_WRITE
)) {
3891 "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3896 switch (fh
->map_mode
) {
3898 case ZORAN_MAP_MODE_JPG_REC
:
3899 case ZORAN_MAP_MODE_JPG_PLAY
:
3902 mutex_lock(&zr
->resource_lock
);
3904 /* Map the MJPEG buffers */
3905 if (!fh
->jpg_buffers
.allocated
) {
3908 "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
3911 goto jpg_mmap_unlock_and_return
;
3914 first
= offset
/ fh
->jpg_buffers
.buffer_size
;
3915 last
= first
- 1 + size
/ fh
->jpg_buffers
.buffer_size
;
3916 if (offset
% fh
->jpg_buffers
.buffer_size
!= 0 ||
3917 size
% fh
->jpg_buffers
.buffer_size
!= 0 || first
< 0 ||
3918 last
< 0 || first
>= fh
->jpg_buffers
.num_buffers
||
3919 last
>= fh
->jpg_buffers
.num_buffers
) {
3922 "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3923 ZR_DEVNAME(zr
), offset
, size
,
3924 fh
->jpg_buffers
.buffer_size
,
3925 fh
->jpg_buffers
.num_buffers
);
3927 goto jpg_mmap_unlock_and_return
;
3929 for (i
= first
; i
<= last
; i
++) {
3930 if (fh
->jpg_buffers
.buffer
[i
].map
) {
3933 "%s: mmap(MJPEG) - buffer %d already mapped\n",
3936 goto jpg_mmap_unlock_and_return
;
3940 /* map these buffers (v4l_buffers[i]) */
3941 map
= kmalloc(sizeof(struct zoran_mapping
), GFP_KERNEL
);
3944 goto jpg_mmap_unlock_and_return
;
3949 vma
->vm_ops
= &zoran_vm_ops
;
3950 vma
->vm_flags
|= VM_DONTEXPAND
;
3951 vma
->vm_private_data
= map
;
3953 for (i
= first
; i
<= last
; i
++) {
3955 j
< fh
->jpg_buffers
.buffer_size
/ PAGE_SIZE
;
3958 (le32_to_cpu(fh
->jpg_buffers
.buffer
[i
].
3959 frag_tab
[2 * j
+ 1]) & ~1) << 1;
3964 le32_to_cpu(fh
->jpg_buffers
.
3965 buffer
[i
].frag_tab
[2 * j
]);
3966 /* should just be pos on i386 */
3967 page
= virt_to_phys(bus_to_virt(pos
))
3969 if (remap_pfn_range(vma
, start
, page
,
3970 todo
, PAGE_SHARED
)) {
3973 "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
3976 goto jpg_mmap_unlock_and_return
;
3982 if (le32_to_cpu(fh
->jpg_buffers
.buffer
[i
].
3983 frag_tab
[2 * j
+ 1]) & 1)
3984 break; /* was last fragment */
3986 fh
->jpg_buffers
.buffer
[i
].map
= map
;
3991 jpg_mmap_unlock_and_return
:
3992 mutex_unlock(&zr
->resource_lock
);
3996 case ZORAN_MAP_MODE_RAW
:
3998 mutex_lock(&zr
->resource_lock
);
4000 /* Map the V4L buffers */
4001 if (!fh
->v4l_buffers
.allocated
) {
4004 "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
4007 goto v4l_mmap_unlock_and_return
;
4010 first
= offset
/ fh
->v4l_buffers
.buffer_size
;
4011 last
= first
- 1 + size
/ fh
->v4l_buffers
.buffer_size
;
4012 if (offset
% fh
->v4l_buffers
.buffer_size
!= 0 ||
4013 size
% fh
->v4l_buffers
.buffer_size
!= 0 || first
< 0 ||
4014 last
< 0 || first
>= fh
->v4l_buffers
.num_buffers
||
4015 last
>= fh
->v4l_buffers
.buffer_size
) {
4018 "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
4019 ZR_DEVNAME(zr
), offset
, size
,
4020 fh
->v4l_buffers
.buffer_size
,
4021 fh
->v4l_buffers
.num_buffers
);
4023 goto v4l_mmap_unlock_and_return
;
4025 for (i
= first
; i
<= last
; i
++) {
4026 if (fh
->v4l_buffers
.buffer
[i
].map
) {
4029 "%s: mmap(V4L) - buffer %d already mapped\n",
4032 goto v4l_mmap_unlock_and_return
;
4036 /* map these buffers (v4l_buffers[i]) */
4037 map
= kmalloc(sizeof(struct zoran_mapping
), GFP_KERNEL
);
4040 goto v4l_mmap_unlock_and_return
;
4045 vma
->vm_ops
= &zoran_vm_ops
;
4046 vma
->vm_flags
|= VM_DONTEXPAND
;
4047 vma
->vm_private_data
= map
;
4049 for (i
= first
; i
<= last
; i
++) {
4051 if (todo
> fh
->v4l_buffers
.buffer_size
)
4052 todo
= fh
->v4l_buffers
.buffer_size
;
4053 page
= fh
->v4l_buffers
.buffer
[i
].fbuffer_phys
;
4054 if (remap_pfn_range(vma
, start
, page
>> PAGE_SHIFT
,
4055 todo
, PAGE_SHARED
)) {
4058 "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
4061 goto v4l_mmap_unlock_and_return
;
4065 fh
->v4l_buffers
.buffer
[i
].map
= map
;
4069 v4l_mmap_unlock_and_return
:
4070 mutex_unlock(&zr
->resource_lock
);
4077 "%s: zoran_mmap() - internal error - unknown map mode %d\n",
4078 ZR_DEVNAME(zr
), fh
->map_mode
);
4085 static const struct v4l2_ioctl_ops zoran_ioctl_ops
= {
4086 .vidioc_querycap
= zoran_querycap
,
4087 .vidioc_cropcap
= zoran_cropcap
,
4088 .vidioc_s_crop
= zoran_s_crop
,
4089 .vidioc_g_crop
= zoran_g_crop
,
4090 .vidioc_enum_input
= zoran_enum_input
,
4091 .vidioc_g_input
= zoran_g_input
,
4092 .vidioc_s_input
= zoran_s_input
,
4093 .vidioc_enum_output
= zoran_enum_output
,
4094 .vidioc_g_output
= zoran_g_output
,
4095 .vidioc_s_output
= zoran_s_output
,
4096 .vidioc_g_fbuf
= zoran_g_fbuf
,
4097 .vidioc_s_fbuf
= zoran_s_fbuf
,
4098 .vidioc_g_std
= zoran_g_std
,
4099 .vidioc_s_std
= zoran_s_std
,
4100 .vidioc_g_jpegcomp
= zoran_g_jpegcomp
,
4101 .vidioc_s_jpegcomp
= zoran_s_jpegcomp
,
4102 .vidioc_overlay
= zoran_overlay
,
4103 .vidioc_reqbufs
= zoran_reqbufs
,
4104 .vidioc_querybuf
= zoran_querybuf
,
4105 .vidioc_qbuf
= zoran_qbuf
,
4106 .vidioc_dqbuf
= zoran_dqbuf
,
4107 .vidioc_streamon
= zoran_streamon
,
4108 .vidioc_streamoff
= zoran_streamoff
,
4109 .vidioc_enum_fmt_vid_cap
= zoran_enum_fmt_vid_cap
,
4110 .vidioc_enum_fmt_vid_out
= zoran_enum_fmt_vid_out
,
4111 .vidioc_enum_fmt_vid_overlay
= zoran_enum_fmt_vid_overlay
,
4112 .vidioc_g_fmt_vid_cap
= zoran_g_fmt_vid_cap
,
4113 .vidioc_g_fmt_vid_out
= zoran_g_fmt_vid_out
,
4114 .vidioc_g_fmt_vid_overlay
= zoran_g_fmt_vid_overlay
,
4115 .vidioc_s_fmt_vid_cap
= zoran_s_fmt_vid_cap
,
4116 .vidioc_s_fmt_vid_out
= zoran_s_fmt_vid_out
,
4117 .vidioc_s_fmt_vid_overlay
= zoran_s_fmt_vid_overlay
,
4118 .vidioc_try_fmt_vid_cap
= zoran_try_fmt_vid_cap
,
4119 .vidioc_try_fmt_vid_out
= zoran_try_fmt_vid_out
,
4120 .vidioc_try_fmt_vid_overlay
= zoran_try_fmt_vid_overlay
,
4121 .vidioc_queryctrl
= zoran_queryctrl
,
4122 .vidioc_s_ctrl
= zoran_s_ctrl
,
4123 .vidioc_g_ctrl
= zoran_g_ctrl
,
4124 .vidioc_default
= zoran_default
,
4127 static const struct v4l2_file_operations zoran_fops
= {
4128 .owner
= THIS_MODULE
,
4130 .release
= zoran_close
,
4131 .ioctl
= video_ioctl2
,
4133 .write
= zoran_write
,
4138 struct video_device zoran_template __devinitdata
= {
4140 .fops
= &zoran_fops
,
4141 .ioctl_ops
= &zoran_ioctl_ops
,
4142 .release
= &zoran_vdev_release
,
4143 .tvnorms
= V4L2_STD_NTSC
| V4L2_STD_PAL
| V4L2_STD_SECAM
,