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/init.h>
48 #include <linux/module.h>
49 #include <linux/delay.h>
50 #include <linux/slab.h>
51 #include <linux/pci.h>
52 #include <linux/vmalloc.h>
53 #include <linux/wait.h>
55 #include <linux/interrupt.h>
56 #include <linux/i2c.h>
57 #include <linux/i2c-algo-bit.h>
59 #include <linux/spinlock.h>
61 #include <linux/videodev2.h>
62 #include <media/v4l2-common.h>
63 #include <media/v4l2-ioctl.h>
64 #include "videocodec.h"
66 #include <asm/byteorder.h>
68 #include <asm/uaccess.h>
69 #include <linux/proc_fs.h>
71 #include <linux/mutex.h>
73 #include "zoran_device.h"
74 #include "zoran_card.h"
77 const struct zoran_format zoran_formats
[] = {
79 .name
= "15-bit RGB LE",
80 .fourcc
= V4L2_PIX_FMT_RGB555
,
81 .colorspace
= V4L2_COLORSPACE_SRGB
,
83 .flags
= ZORAN_FORMAT_CAPTURE
|
85 .vfespfr
= ZR36057_VFESPFR_RGB555
|ZR36057_VFESPFR_ErrDif
|
86 ZR36057_VFESPFR_LittleEndian
,
88 .name
= "15-bit RGB BE",
89 .fourcc
= V4L2_PIX_FMT_RGB555X
,
90 .colorspace
= V4L2_COLORSPACE_SRGB
,
92 .flags
= ZORAN_FORMAT_CAPTURE
|
94 .vfespfr
= ZR36057_VFESPFR_RGB555
|ZR36057_VFESPFR_ErrDif
,
96 .name
= "16-bit RGB LE",
97 .fourcc
= V4L2_PIX_FMT_RGB565
,
98 .colorspace
= V4L2_COLORSPACE_SRGB
,
100 .flags
= ZORAN_FORMAT_CAPTURE
|
101 ZORAN_FORMAT_OVERLAY
,
102 .vfespfr
= ZR36057_VFESPFR_RGB565
|ZR36057_VFESPFR_ErrDif
|
103 ZR36057_VFESPFR_LittleEndian
,
105 .name
= "16-bit RGB BE",
106 .fourcc
= V4L2_PIX_FMT_RGB565X
,
107 .colorspace
= V4L2_COLORSPACE_SRGB
,
109 .flags
= ZORAN_FORMAT_CAPTURE
|
110 ZORAN_FORMAT_OVERLAY
,
111 .vfespfr
= ZR36057_VFESPFR_RGB565
|ZR36057_VFESPFR_ErrDif
,
113 .name
= "24-bit RGB",
114 .fourcc
= V4L2_PIX_FMT_BGR24
,
115 .colorspace
= V4L2_COLORSPACE_SRGB
,
117 .flags
= ZORAN_FORMAT_CAPTURE
|
118 ZORAN_FORMAT_OVERLAY
,
119 .vfespfr
= ZR36057_VFESPFR_RGB888
|ZR36057_VFESPFR_Pack24
,
121 .name
= "32-bit RGB LE",
122 .fourcc
= V4L2_PIX_FMT_BGR32
,
123 .colorspace
= V4L2_COLORSPACE_SRGB
,
125 .flags
= ZORAN_FORMAT_CAPTURE
|
126 ZORAN_FORMAT_OVERLAY
,
127 .vfespfr
= ZR36057_VFESPFR_RGB888
|ZR36057_VFESPFR_LittleEndian
,
129 .name
= "32-bit RGB BE",
130 .fourcc
= V4L2_PIX_FMT_RGB32
,
131 .colorspace
= V4L2_COLORSPACE_SRGB
,
133 .flags
= ZORAN_FORMAT_CAPTURE
|
134 ZORAN_FORMAT_OVERLAY
,
135 .vfespfr
= ZR36057_VFESPFR_RGB888
,
137 .name
= "4:2:2, packed, YUYV",
138 .fourcc
= V4L2_PIX_FMT_YUYV
,
139 .colorspace
= V4L2_COLORSPACE_SMPTE170M
,
141 .flags
= ZORAN_FORMAT_CAPTURE
|
142 ZORAN_FORMAT_OVERLAY
,
143 .vfespfr
= ZR36057_VFESPFR_YUV422
,
145 .name
= "4:2:2, packed, UYVY",
146 .fourcc
= V4L2_PIX_FMT_UYVY
,
147 .colorspace
= V4L2_COLORSPACE_SMPTE170M
,
149 .flags
= ZORAN_FORMAT_CAPTURE
|
150 ZORAN_FORMAT_OVERLAY
,
151 .vfespfr
= ZR36057_VFESPFR_YUV422
|ZR36057_VFESPFR_LittleEndian
,
153 .name
= "Hardware-encoded Motion-JPEG",
154 .fourcc
= V4L2_PIX_FMT_MJPEG
,
155 .colorspace
= V4L2_COLORSPACE_SMPTE170M
,
157 .flags
= ZORAN_FORMAT_CAPTURE
|
158 ZORAN_FORMAT_PLAYBACK
|
159 ZORAN_FORMAT_COMPRESSED
,
162 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
164 /* small helper function for calculating buffersizes for v4l2
165 * we calculate the nearest higher power-of-two, which
166 * will be the recommended buffersize */
168 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings
*settings
)
170 __u8 div
= settings
->VerDcm
* settings
->HorDcm
* settings
->TmpDcm
;
171 __u32 num
= (1024 * 512) / (div
);
180 if (result
> jpg_bufsize
)
187 /* forward references */
188 static void v4l_fbuffer_free(struct zoran_fh
*fh
);
189 static void jpg_fbuffer_free(struct zoran_fh
*fh
);
191 /* Set mapping mode */
192 static void map_mode_raw(struct zoran_fh
*fh
)
194 fh
->map_mode
= ZORAN_MAP_MODE_RAW
;
195 fh
->buffers
.buffer_size
= v4l_bufsize
;
196 fh
->buffers
.num_buffers
= v4l_nbufs
;
198 static void map_mode_jpg(struct zoran_fh
*fh
, int play
)
200 fh
->map_mode
= play
? ZORAN_MAP_MODE_JPG_PLAY
: ZORAN_MAP_MODE_JPG_REC
;
201 fh
->buffers
.buffer_size
= jpg_bufsize
;
202 fh
->buffers
.num_buffers
= jpg_nbufs
;
204 static inline const char *mode_name(enum zoran_map_mode mode
)
206 return mode
== ZORAN_MAP_MODE_RAW
? "V4L" : "JPG";
210 * Allocate the V4L grab buffers
212 * These have to be pysically contiguous.
215 static int v4l_fbuffer_alloc(struct zoran_fh
*fh
)
217 struct zoran
*zr
= fh
->zr
;
221 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++) {
222 if (fh
->buffers
.buffer
[i
].v4l
.fbuffer
)
225 "%s: %s - buffer %d already allocated!?\n",
226 ZR_DEVNAME(zr
), __func__
, i
);
229 mem
= kmalloc(fh
->buffers
.buffer_size
,
230 GFP_KERNEL
| __GFP_NOWARN
);
234 "%s: %s - kmalloc for V4L buf %d failed\n",
235 ZR_DEVNAME(zr
), __func__
, i
);
236 v4l_fbuffer_free(fh
);
239 fh
->buffers
.buffer
[i
].v4l
.fbuffer
= mem
;
240 fh
->buffers
.buffer
[i
].v4l
.fbuffer_phys
= virt_to_phys(mem
);
241 fh
->buffers
.buffer
[i
].v4l
.fbuffer_bus
= virt_to_bus(mem
);
242 for (off
= 0; off
< fh
->buffers
.buffer_size
;
244 SetPageReserved(virt_to_page(mem
+ off
));
247 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
248 ZR_DEVNAME(zr
), __func__
, i
, (unsigned long) mem
,
249 (unsigned long long)virt_to_bus(mem
));
252 fh
->buffers
.allocated
= 1;
257 /* free the V4L grab buffers */
258 static void v4l_fbuffer_free(struct zoran_fh
*fh
)
260 struct zoran
*zr
= fh
->zr
;
264 dprintk(4, KERN_INFO
"%s: %s\n", ZR_DEVNAME(zr
), __func__
);
266 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++) {
267 if (!fh
->buffers
.buffer
[i
].v4l
.fbuffer
)
270 mem
= fh
->buffers
.buffer
[i
].v4l
.fbuffer
;
271 for (off
= 0; off
< fh
->buffers
.buffer_size
;
273 ClearPageReserved(virt_to_page(mem
+ off
));
274 kfree(fh
->buffers
.buffer
[i
].v4l
.fbuffer
);
275 fh
->buffers
.buffer
[i
].v4l
.fbuffer
= NULL
;
278 fh
->buffers
.allocated
= 0;
282 * Allocate the MJPEG grab buffers.
284 * If a Natoma chipset is present and this is a revision 1 zr36057,
285 * each MJPEG buffer needs to be physically contiguous.
286 * (RJ: This statement is from Dave Perks' original driver,
287 * I could never check it because I have a zr36067)
289 * RJ: The contents grab buffers needs never be accessed in the driver.
290 * Therefore there is no need to allocate them with vmalloc in order
291 * to get a contiguous virtual memory space.
292 * I don't understand why many other drivers first allocate them with
293 * vmalloc (which uses internally also get_zeroed_page, but delivers you
294 * virtual addresses) and then again have to make a lot of efforts
295 * to get the physical address.
298 * On big-endian architectures (such as ppc) some extra steps
299 * are needed. When reading and writing to the stat_com array
300 * and fragment buffers, the device expects to see little-
301 * endian values. The use of cpu_to_le32() and le32_to_cpu()
302 * in this function (and one or two others in zoran_device.c)
303 * ensure that these values are always stored in little-endian
304 * form, regardless of architecture. The zr36057 does Very Bad
305 * Things on big endian architectures if the stat_com array
306 * and fragment buffers are not little-endian.
309 static int jpg_fbuffer_alloc(struct zoran_fh
*fh
)
311 struct zoran
*zr
= fh
->zr
;
315 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++) {
316 if (fh
->buffers
.buffer
[i
].jpg
.frag_tab
)
319 "%s: %s - buffer %d already allocated!?\n",
320 ZR_DEVNAME(zr
), __func__
, i
);
322 /* Allocate fragment table for this buffer */
324 mem
= (void *)get_zeroed_page(GFP_KERNEL
);
328 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
329 ZR_DEVNAME(zr
), __func__
, i
);
330 jpg_fbuffer_free(fh
);
333 fh
->buffers
.buffer
[i
].jpg
.frag_tab
= (__le32
*)mem
;
334 fh
->buffers
.buffer
[i
].jpg
.frag_tab_bus
= virt_to_bus(mem
);
336 if (fh
->buffers
.need_contiguous
) {
337 mem
= kmalloc(fh
->buffers
.buffer_size
, GFP_KERNEL
);
341 "%s: %s - kmalloc failed for buffer %d\n",
342 ZR_DEVNAME(zr
), __func__
, i
);
343 jpg_fbuffer_free(fh
);
346 fh
->buffers
.buffer
[i
].jpg
.frag_tab
[0] =
347 cpu_to_le32(virt_to_bus(mem
));
348 fh
->buffers
.buffer
[i
].jpg
.frag_tab
[1] =
349 cpu_to_le32((fh
->buffers
.buffer_size
>> 1) | 1);
350 for (off
= 0; off
< fh
->buffers
.buffer_size
; off
+= PAGE_SIZE
)
351 SetPageReserved(virt_to_page(mem
+ off
));
353 /* jpg_bufsize is already page aligned */
354 for (j
= 0; j
< fh
->buffers
.buffer_size
/ PAGE_SIZE
; j
++) {
355 mem
= (void *)get_zeroed_page(GFP_KERNEL
);
359 "%s: %s - get_zeroed_page failed for buffer %d\n",
360 ZR_DEVNAME(zr
), __func__
, i
);
361 jpg_fbuffer_free(fh
);
365 fh
->buffers
.buffer
[i
].jpg
.frag_tab
[2 * j
] =
366 cpu_to_le32(virt_to_bus(mem
));
367 fh
->buffers
.buffer
[i
].jpg
.frag_tab
[2 * j
+ 1] =
368 cpu_to_le32((PAGE_SIZE
>> 2) << 1);
369 SetPageReserved(virt_to_page(mem
));
372 fh
->buffers
.buffer
[i
].jpg
.frag_tab
[2 * j
- 1] |= cpu_to_le32(1);
377 KERN_DEBUG
"%s: %s - %d KB allocated\n",
378 ZR_DEVNAME(zr
), __func__
,
379 (fh
->buffers
.num_buffers
* fh
->buffers
.buffer_size
) >> 10);
381 fh
->buffers
.allocated
= 1;
386 /* free the MJPEG grab buffers */
387 static void jpg_fbuffer_free(struct zoran_fh
*fh
)
389 struct zoran
*zr
= fh
->zr
;
393 struct zoran_buffer
*buffer
;
395 dprintk(4, KERN_DEBUG
"%s: %s\n", ZR_DEVNAME(zr
), __func__
);
397 for (i
= 0, buffer
= &fh
->buffers
.buffer
[0];
398 i
< fh
->buffers
.num_buffers
; i
++, buffer
++) {
399 if (!buffer
->jpg
.frag_tab
)
402 if (fh
->buffers
.need_contiguous
) {
403 frag_tab
= buffer
->jpg
.frag_tab
[0];
406 mem
= bus_to_virt(le32_to_cpu(frag_tab
));
407 for (off
= 0; off
< fh
->buffers
.buffer_size
; off
+= PAGE_SIZE
)
408 ClearPageReserved(virt_to_page(mem
+ off
));
410 buffer
->jpg
.frag_tab
[0] = 0;
411 buffer
->jpg
.frag_tab
[1] = 0;
414 for (j
= 0; j
< fh
->buffers
.buffer_size
/ PAGE_SIZE
; j
++) {
415 frag_tab
= buffer
->jpg
.frag_tab
[2 * j
];
419 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab
))));
420 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab
)));
421 buffer
->jpg
.frag_tab
[2 * j
] = 0;
422 buffer
->jpg
.frag_tab
[2 * j
+ 1] = 0;
426 free_page((unsigned long)buffer
->jpg
.frag_tab
);
427 buffer
->jpg
.frag_tab
= NULL
;
430 fh
->buffers
.allocated
= 0;
434 * V4L Buffer grabbing
438 zoran_v4l_set_format (struct zoran_fh
*fh
,
441 const struct zoran_format
*format
)
443 struct zoran
*zr
= fh
->zr
;
446 /* Check size and format of the grab wanted */
448 if (height
< BUZ_MIN_HEIGHT
|| width
< BUZ_MIN_WIDTH
||
449 height
> BUZ_MAX_HEIGHT
|| width
> BUZ_MAX_WIDTH
) {
452 "%s: %s - wrong frame size (%dx%d)\n",
453 ZR_DEVNAME(zr
), __func__
, width
, height
);
457 bpp
= (format
->depth
+ 7) / 8;
459 /* Check against available buffer size */
460 if (height
* width
* bpp
> fh
->buffers
.buffer_size
) {
463 "%s: %s - video buffer size (%d kB) is too small\n",
464 ZR_DEVNAME(zr
), __func__
, fh
->buffers
.buffer_size
>> 10);
468 /* The video front end needs 4-byte alinged line sizes */
470 if ((bpp
== 2 && (width
& 1)) || (bpp
== 3 && (width
& 3))) {
473 "%s: %s - wrong frame alignment\n",
474 ZR_DEVNAME(zr
), __func__
);
478 fh
->v4l_settings
.width
= width
;
479 fh
->v4l_settings
.height
= height
;
480 fh
->v4l_settings
.format
= format
;
481 fh
->v4l_settings
.bytesperline
= bpp
* fh
->v4l_settings
.width
;
486 static int zoran_v4l_queue_frame(struct zoran_fh
*fh
, int num
)
488 struct zoran
*zr
= fh
->zr
;
492 if (!fh
->buffers
.allocated
) {
495 "%s: %s - buffers not yet allocated\n",
496 ZR_DEVNAME(zr
), __func__
);
500 /* No grabbing outside the buffer range! */
501 if (num
>= fh
->buffers
.num_buffers
|| num
< 0) {
504 "%s: %s - buffer %d is out of range\n",
505 ZR_DEVNAME(zr
), __func__
, num
);
509 spin_lock_irqsave(&zr
->spinlock
, flags
);
511 if (fh
->buffers
.active
== ZORAN_FREE
) {
512 if (zr
->v4l_buffers
.active
== ZORAN_FREE
) {
513 zr
->v4l_buffers
= fh
->buffers
;
514 fh
->buffers
.active
= ZORAN_ACTIVE
;
518 "%s: %s - another session is already capturing\n",
519 ZR_DEVNAME(zr
), __func__
);
524 /* make sure a grab isn't going on currently with this buffer */
526 switch (zr
->v4l_buffers
.buffer
[num
].state
) {
529 if (zr
->v4l_buffers
.active
== ZORAN_FREE
) {
530 fh
->buffers
.active
= ZORAN_FREE
;
531 zr
->v4l_buffers
.allocated
= 0;
533 res
= -EBUSY
; /* what are you doing? */
538 "%s: %s - queueing buffer %d in state DONE!?\n",
539 ZR_DEVNAME(zr
), __func__
, num
);
541 /* since there is at least one unused buffer there's room for at least
542 * one more pend[] entry */
543 zr
->v4l_pend
[zr
->v4l_pend_head
++ & V4L_MASK_FRAME
] = num
;
544 zr
->v4l_buffers
.buffer
[num
].state
= BUZ_STATE_PEND
;
545 zr
->v4l_buffers
.buffer
[num
].bs
.length
=
546 fh
->v4l_settings
.bytesperline
*
547 zr
->v4l_settings
.height
;
548 fh
->buffers
.buffer
[num
] = zr
->v4l_buffers
.buffer
[num
];
553 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
555 if (!res
&& zr
->v4l_buffers
.active
== ZORAN_FREE
)
556 zr
->v4l_buffers
.active
= fh
->buffers
.active
;
562 * Sync on a V4L buffer
565 static int v4l_sync(struct zoran_fh
*fh
, int frame
)
567 struct zoran
*zr
= fh
->zr
;
570 if (fh
->buffers
.active
== ZORAN_FREE
) {
573 "%s: %s - no grab active for this session\n",
574 ZR_DEVNAME(zr
), __func__
);
578 /* check passed-in frame number */
579 if (frame
>= fh
->buffers
.num_buffers
|| frame
< 0) {
581 KERN_ERR
"%s: %s - frame %d is invalid\n",
582 ZR_DEVNAME(zr
), __func__
, frame
);
586 /* Check if is buffer was queued at all */
587 if (zr
->v4l_buffers
.buffer
[frame
].state
== BUZ_STATE_USER
) {
590 "%s: %s - attempt to sync on a buffer which was not queued?\n",
591 ZR_DEVNAME(zr
), __func__
);
595 /* wait on this buffer to get ready */
596 if (!wait_event_interruptible_timeout(zr
->v4l_capq
,
597 (zr
->v4l_buffers
.buffer
[frame
].state
!= BUZ_STATE_PEND
), 10*HZ
))
599 if (signal_pending(current
))
602 /* buffer should now be in BUZ_STATE_DONE */
603 if (zr
->v4l_buffers
.buffer
[frame
].state
!= BUZ_STATE_DONE
)
605 KERN_ERR
"%s: %s - internal state error\n",
606 ZR_DEVNAME(zr
), __func__
);
608 zr
->v4l_buffers
.buffer
[frame
].state
= BUZ_STATE_USER
;
609 fh
->buffers
.buffer
[frame
] = zr
->v4l_buffers
.buffer
[frame
];
611 spin_lock_irqsave(&zr
->spinlock
, flags
);
613 /* Check if streaming capture has finished */
614 if (zr
->v4l_pend_tail
== zr
->v4l_pend_head
) {
615 zr36057_set_memgrab(zr
, 0);
616 if (zr
->v4l_buffers
.active
== ZORAN_ACTIVE
) {
617 fh
->buffers
.active
= zr
->v4l_buffers
.active
= ZORAN_FREE
;
618 zr
->v4l_buffers
.allocated
= 0;
622 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
628 * Queue a MJPEG buffer for capture/playback
631 static int zoran_jpg_queue_frame(struct zoran_fh
*fh
, int num
,
632 enum zoran_codec_mode mode
)
634 struct zoran
*zr
= fh
->zr
;
638 /* Check if buffers are allocated */
639 if (!fh
->buffers
.allocated
) {
642 "%s: %s - buffers not yet allocated\n",
643 ZR_DEVNAME(zr
), __func__
);
647 /* No grabbing outside the buffer range! */
648 if (num
>= fh
->buffers
.num_buffers
|| num
< 0) {
651 "%s: %s - buffer %d out of range\n",
652 ZR_DEVNAME(zr
), __func__
, num
);
656 /* what is the codec mode right now? */
657 if (zr
->codec_mode
== BUZ_MODE_IDLE
) {
658 zr
->jpg_settings
= fh
->jpg_settings
;
659 } else if (zr
->codec_mode
!= mode
) {
660 /* wrong codec mode active - invalid */
663 "%s: %s - codec in wrong mode\n",
664 ZR_DEVNAME(zr
), __func__
);
668 if (fh
->buffers
.active
== ZORAN_FREE
) {
669 if (zr
->jpg_buffers
.active
== ZORAN_FREE
) {
670 zr
->jpg_buffers
= fh
->buffers
;
671 fh
->buffers
.active
= ZORAN_ACTIVE
;
675 "%s: %s - another session is already capturing\n",
676 ZR_DEVNAME(zr
), __func__
);
681 if (!res
&& zr
->codec_mode
== BUZ_MODE_IDLE
) {
682 /* Ok load up the jpeg codec */
683 zr36057_enable_jpg(zr
, mode
);
686 spin_lock_irqsave(&zr
->spinlock
, flags
);
689 switch (zr
->jpg_buffers
.buffer
[num
].state
) {
693 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
694 ZR_DEVNAME(zr
), __func__
);
696 /* since there is at least one unused buffer there's room for at
697 *least one more pend[] entry */
698 zr
->jpg_pend
[zr
->jpg_que_head
++ & BUZ_MASK_FRAME
] = num
;
699 zr
->jpg_buffers
.buffer
[num
].state
= BUZ_STATE_PEND
;
700 fh
->buffers
.buffer
[num
] = zr
->jpg_buffers
.buffer
[num
];
701 zoran_feed_stat_com(zr
);
706 if (zr
->jpg_buffers
.active
== ZORAN_FREE
) {
707 fh
->buffers
.active
= ZORAN_FREE
;
708 zr
->jpg_buffers
.allocated
= 0;
710 res
= -EBUSY
; /* what are you doing? */
715 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
717 if (!res
&& zr
->jpg_buffers
.active
== ZORAN_FREE
)
718 zr
->jpg_buffers
.active
= fh
->buffers
.active
;
723 static int jpg_qbuf(struct zoran_fh
*fh
, int frame
, enum zoran_codec_mode mode
)
725 struct zoran
*zr
= fh
->zr
;
728 /* Does the user want to stop streaming? */
730 if (zr
->codec_mode
== mode
) {
731 if (fh
->buffers
.active
== ZORAN_FREE
) {
734 "%s: %s(-1) - session not active\n",
735 ZR_DEVNAME(zr
), __func__
);
738 fh
->buffers
.active
= zr
->jpg_buffers
.active
= ZORAN_FREE
;
739 zr
->jpg_buffers
.allocated
= 0;
740 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
745 "%s: %s - stop streaming but not in streaming mode\n",
746 ZR_DEVNAME(zr
), __func__
);
751 if ((res
= zoran_jpg_queue_frame(fh
, frame
, mode
)))
754 /* Start the jpeg codec when the first frame is queued */
755 if (!res
&& zr
->jpg_que_head
== 1)
762 * Sync on a MJPEG buffer
765 static int jpg_sync(struct zoran_fh
*fh
, struct zoran_sync
*bs
)
767 struct zoran
*zr
= fh
->zr
;
771 if (fh
->buffers
.active
== ZORAN_FREE
) {
774 "%s: %s - capture is not currently active\n",
775 ZR_DEVNAME(zr
), __func__
);
778 if (zr
->codec_mode
!= BUZ_MODE_MOTION_DECOMPRESS
&&
779 zr
->codec_mode
!= BUZ_MODE_MOTION_COMPRESS
) {
782 "%s: %s - codec not in streaming mode\n",
783 ZR_DEVNAME(zr
), __func__
);
786 if (!wait_event_interruptible_timeout(zr
->jpg_capq
,
787 (zr
->jpg_que_tail
!= zr
->jpg_dma_tail
||
788 zr
->jpg_dma_tail
== zr
->jpg_dma_head
),
792 btand(~ZR36057_JMC_Go_en
, ZR36057_JMC
);
794 zr
->codec
->control(zr
->codec
, CODEC_G_STATUS
,
798 "%s: %s - timeout: codec isr=0x%02x\n",
799 ZR_DEVNAME(zr
), __func__
, isr
);
804 if (signal_pending(current
))
807 spin_lock_irqsave(&zr
->spinlock
, flags
);
809 if (zr
->jpg_dma_tail
!= zr
->jpg_dma_head
)
810 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
++ & BUZ_MASK_FRAME
];
812 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
814 /* buffer should now be in BUZ_STATE_DONE */
815 if (zr
->jpg_buffers
.buffer
[frame
].state
!= BUZ_STATE_DONE
)
817 KERN_ERR
"%s: %s - internal state error\n",
818 ZR_DEVNAME(zr
), __func__
);
820 *bs
= zr
->jpg_buffers
.buffer
[frame
].bs
;
822 zr
->jpg_buffers
.buffer
[frame
].state
= BUZ_STATE_USER
;
823 fh
->buffers
.buffer
[frame
] = zr
->jpg_buffers
.buffer
[frame
];
825 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
830 static void zoran_open_init_session(struct zoran_fh
*fh
)
833 struct zoran
*zr
= fh
->zr
;
835 /* Per default, map the V4L Buffers */
838 /* take over the card's current settings */
839 fh
->overlay_settings
= zr
->overlay_settings
;
840 fh
->overlay_settings
.is_set
= 0;
841 fh
->overlay_settings
.format
= zr
->overlay_settings
.format
;
842 fh
->overlay_active
= ZORAN_FREE
;
845 fh
->v4l_settings
= zr
->v4l_settings
;
847 fh
->jpg_settings
= zr
->jpg_settings
;
850 memset(&fh
->buffers
, 0, sizeof(fh
->buffers
));
851 for (i
= 0; i
< MAX_FRAME
; i
++) {
852 fh
->buffers
.buffer
[i
].state
= BUZ_STATE_USER
; /* nothing going on */
853 fh
->buffers
.buffer
[i
].bs
.frame
= i
;
855 fh
->buffers
.allocated
= 0;
856 fh
->buffers
.active
= ZORAN_FREE
;
859 static void zoran_close_end_session(struct zoran_fh
*fh
)
861 struct zoran
*zr
= fh
->zr
;
864 if (fh
->overlay_active
!= ZORAN_FREE
) {
865 fh
->overlay_active
= zr
->overlay_active
= ZORAN_FREE
;
866 zr
->v4l_overlay_active
= 0;
867 if (!zr
->v4l_memgrab_active
)
868 zr36057_overlay(zr
, 0);
869 zr
->overlay_mask
= NULL
;
872 if (fh
->map_mode
== ZORAN_MAP_MODE_RAW
) {
874 if (fh
->buffers
.active
!= ZORAN_FREE
) {
877 spin_lock_irqsave(&zr
->spinlock
, flags
);
878 zr36057_set_memgrab(zr
, 0);
879 zr
->v4l_buffers
.allocated
= 0;
880 zr
->v4l_buffers
.active
= fh
->buffers
.active
= ZORAN_FREE
;
881 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
885 if (fh
->buffers
.allocated
)
886 v4l_fbuffer_free(fh
);
889 if (fh
->buffers
.active
!= ZORAN_FREE
) {
890 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
891 zr
->jpg_buffers
.allocated
= 0;
892 zr
->jpg_buffers
.active
= fh
->buffers
.active
= ZORAN_FREE
;
896 if (fh
->buffers
.allocated
)
897 jpg_fbuffer_free(fh
);
902 * Open a zoran card. Right now the flags stuff is just playing
905 static int zoran_open(struct file
*file
)
907 struct zoran
*zr
= video_drvdata(file
);
909 int res
, first_open
= 0;
911 dprintk(2, KERN_INFO
"%s: %s(%s, pid=[%d]), users(-)=%d\n",
912 ZR_DEVNAME(zr
), __func__
, current
->comm
, task_pid_nr(current
), zr
->user
+ 1);
914 mutex_lock(&zr
->other_lock
);
916 if (zr
->user
>= 2048) {
917 dprintk(1, KERN_ERR
"%s: too many users (%d) on device\n",
918 ZR_DEVNAME(zr
), zr
->user
);
923 /* now, create the open()-specific file_ops struct */
924 fh
= kzalloc(sizeof(struct zoran_fh
), GFP_KERNEL
);
928 "%s: %s - allocation of zoran_fh failed\n",
929 ZR_DEVNAME(zr
), __func__
);
933 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
936 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL
);
937 if (!fh
->overlay_mask
) {
940 "%s: %s - allocation of overlay_mask failed\n",
941 ZR_DEVNAME(zr
), __func__
);
949 /*mutex_unlock(&zr->resource_lock);*/
951 /* default setup - TODO: look at flags */
952 if (first_open
) { /* First device open */
954 zoran_open_init_params(zr
);
955 zoran_init_hardware(zr
);
957 btor(ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
960 /* set file_ops stuff */
961 file
->private_data
= fh
;
963 zoran_open_init_session(fh
);
964 mutex_unlock(&zr
->other_lock
);
971 mutex_unlock(&zr
->other_lock
);
973 dprintk(2, KERN_INFO
"%s: open failed (%d), users(-)=%d\n",
974 ZR_DEVNAME(zr
), res
, zr
->user
);
980 zoran_close(struct file
*file
)
982 struct zoran_fh
*fh
= file
->private_data
;
983 struct zoran
*zr
= fh
->zr
;
985 dprintk(2, KERN_INFO
"%s: %s(%s, pid=[%d]), users(+)=%d\n",
986 ZR_DEVNAME(zr
), __func__
, current
->comm
, task_pid_nr(current
), zr
->user
- 1);
988 /* kernel locks (fs/device.c), so don't do that ourselves
989 * (prevents deadlocks) */
990 mutex_lock(&zr
->other_lock
);
992 zoran_close_end_session(fh
);
994 if (zr
->user
-- == 1) { /* Last process */
995 /* Clean up JPEG process */
996 wake_up_interruptible(&zr
->jpg_capq
);
997 zr36057_enable_jpg(zr
, BUZ_MODE_IDLE
);
998 zr
->jpg_buffers
.allocated
= 0;
999 zr
->jpg_buffers
.active
= ZORAN_FREE
;
1001 /* disable interrupts */
1002 btand(~ZR36057_ICR_IntPinEn
, ZR36057_ICR
);
1004 if (zr36067_debug
> 1)
1005 print_interrupts(zr
);
1008 zr
->v4l_overlay_active
= 0;
1009 zr36057_overlay(zr
, 0);
1010 zr
->overlay_mask
= NULL
;
1013 wake_up_interruptible(&zr
->v4l_capq
);
1014 zr36057_set_memgrab(zr
, 0);
1015 zr
->v4l_buffers
.allocated
= 0;
1016 zr
->v4l_buffers
.active
= ZORAN_FREE
;
1017 zoran_set_pci_master(zr
, 0);
1019 if (!pass_through
) { /* Switch to color bar */
1020 decoder_call(zr
, video
, s_stream
, 0);
1021 encoder_call(zr
, video
, s_routing
, 2, 0, 0);
1024 mutex_unlock(&zr
->other_lock
);
1026 file
->private_data
= NULL
;
1027 kfree(fh
->overlay_mask
);
1030 dprintk(4, KERN_INFO
"%s: %s done\n", ZR_DEVNAME(zr
), __func__
);
1037 zoran_read (struct file
*file
,
1042 /* we simply don't support read() (yet)... */
1048 zoran_write (struct file
*file
,
1049 const char __user
*data
,
1053 /* ...and the same goes for write() */
1058 static int setup_fbuffer(struct zoran_fh
*fh
,
1060 const struct zoran_format
*fmt
,
1065 struct zoran
*zr
= fh
->zr
;
1067 /* (Ronald) v4l/v4l2 guidelines */
1068 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
1071 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1072 ALi Magik (that needs very low latency while the card needs a
1073 higher value always) */
1075 if (pci_pci_problems
& (PCIPCI_FAIL
| PCIAGP_FAIL
| PCIPCI_ALIMAGIK
))
1078 /* we need a bytesperline value, even if not given */
1080 bytesperline
= width
* ((fmt
->depth
+ 7) & ~7) / 8;
1083 if (zr
->overlay_active
) {
1084 /* dzjee... stupid users... don't even bother to turn off
1085 * overlay before changing the memory location...
1086 * normally, we would return errors here. However, one of
1087 * the tools that does this is... xawtv! and since xawtv
1088 * is used by +/- 99% of the users, we'd rather be user-
1089 * friendly and silently do as if nothing went wrong */
1092 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1093 ZR_DEVNAME(zr
), __func__
);
1094 zr36057_overlay(zr
, 0);
1098 if (!(fmt
->flags
& ZORAN_FORMAT_OVERLAY
)) {
1101 "%s: %s - no valid overlay format given\n",
1102 ZR_DEVNAME(zr
), __func__
);
1105 if (height
<= 0 || width
<= 0 || bytesperline
<= 0) {
1108 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1109 ZR_DEVNAME(zr
), __func__
, width
, height
, bytesperline
);
1112 if (bytesperline
& 3) {
1115 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1116 ZR_DEVNAME(zr
), __func__
, bytesperline
);
1120 zr
->vbuf_base
= (void *) ((unsigned long) base
& ~3);
1121 zr
->vbuf_height
= height
;
1122 zr
->vbuf_width
= width
;
1123 zr
->vbuf_depth
= fmt
->depth
;
1124 zr
->overlay_settings
.format
= fmt
;
1125 zr
->vbuf_bytesperline
= bytesperline
;
1127 /* The user should set new window parameters */
1128 zr
->overlay_settings
.is_set
= 0;
1134 static int setup_window(struct zoran_fh
*fh
,
1139 struct v4l2_clip __user
*clips
,
1140 unsigned int clipcount
,
1141 void __user
*bitmap
)
1143 struct zoran
*zr
= fh
->zr
;
1144 struct v4l2_clip
*vcp
= NULL
;
1148 if (!zr
->vbuf_base
) {
1151 "%s: %s - frame buffer has to be set first\n",
1152 ZR_DEVNAME(zr
), __func__
);
1156 if (!fh
->overlay_settings
.format
) {
1159 "%s: %s - no overlay format set\n",
1160 ZR_DEVNAME(zr
), __func__
);
1164 if (clipcount
> 2048) {
1167 "%s: %s - invalid clipcount\n",
1168 ZR_DEVNAME(zr
), __func__
);
1173 * The video front end needs 4-byte alinged line sizes, we correct that
1174 * silently here if necessary
1176 if (zr
->vbuf_depth
== 15 || zr
->vbuf_depth
== 16) {
1177 end
= (x
+ width
) & ~1; /* round down */
1178 x
= (x
+ 1) & ~1; /* round up */
1182 if (zr
->vbuf_depth
== 24) {
1183 end
= (x
+ width
) & ~3; /* round down */
1184 x
= (x
+ 3) & ~3; /* round up */
1188 if (width
> BUZ_MAX_WIDTH
)
1189 width
= BUZ_MAX_WIDTH
;
1190 if (height
> BUZ_MAX_HEIGHT
)
1191 height
= BUZ_MAX_HEIGHT
;
1193 /* Check for invalid parameters */
1194 if (width
< BUZ_MIN_WIDTH
|| height
< BUZ_MIN_HEIGHT
||
1195 width
> BUZ_MAX_WIDTH
|| height
> BUZ_MAX_HEIGHT
) {
1198 "%s: %s - width = %d or height = %d invalid\n",
1199 ZR_DEVNAME(zr
), __func__
, width
, height
);
1203 fh
->overlay_settings
.x
= x
;
1204 fh
->overlay_settings
.y
= y
;
1205 fh
->overlay_settings
.width
= width
;
1206 fh
->overlay_settings
.height
= height
;
1207 fh
->overlay_settings
.clipcount
= clipcount
;
1210 * If an overlay is running, we have to switch it off
1211 * and switch it on again in order to get the new settings in effect.
1213 * We also want to avoid that the overlay mask is written
1214 * when an overlay is running.
1217 on
= zr
->v4l_overlay_active
&& !zr
->v4l_memgrab_active
&&
1218 zr
->overlay_active
!= ZORAN_FREE
&&
1219 fh
->overlay_active
!= ZORAN_FREE
;
1221 zr36057_overlay(zr
, 0);
1224 * Write the overlay mask if clips are wanted.
1225 * We prefer a bitmap.
1228 /* fake value - it just means we want clips */
1229 fh
->overlay_settings
.clipcount
= 1;
1231 if (copy_from_user(fh
->overlay_mask
, bitmap
,
1232 (width
* height
+ 7) / 8)) {
1235 } else if (clipcount
) {
1236 /* write our own bitmap from the clips */
1237 vcp
= vmalloc(sizeof(struct v4l2_clip
) * (clipcount
+ 4));
1241 "%s: %s - Alloc of clip mask failed\n",
1242 ZR_DEVNAME(zr
), __func__
);
1246 (vcp
, clips
, sizeof(struct v4l2_clip
) * clipcount
)) {
1250 write_overlay_mask(fh
, vcp
, clipcount
);
1254 fh
->overlay_settings
.is_set
= 1;
1255 if (fh
->overlay_active
!= ZORAN_FREE
&&
1256 zr
->overlay_active
!= ZORAN_FREE
)
1257 zr
->overlay_settings
= fh
->overlay_settings
;
1260 zr36057_overlay(zr
, 1);
1262 /* Make sure the changes come into effect */
1263 return wait_grab_pending(zr
);
1266 static int setup_overlay(struct zoran_fh
*fh
, int on
)
1268 struct zoran
*zr
= fh
->zr
;
1270 /* If there is nothing to do, return immediately */
1271 if ((on
&& fh
->overlay_active
!= ZORAN_FREE
) ||
1272 (!on
&& fh
->overlay_active
== ZORAN_FREE
))
1275 /* check whether we're touching someone else's overlay */
1276 if (on
&& zr
->overlay_active
!= ZORAN_FREE
&&
1277 fh
->overlay_active
== ZORAN_FREE
) {
1280 "%s: %s - overlay is already active for another session\n",
1281 ZR_DEVNAME(zr
), __func__
);
1284 if (!on
&& zr
->overlay_active
!= ZORAN_FREE
&&
1285 fh
->overlay_active
== ZORAN_FREE
) {
1288 "%s: %s - you cannot cancel someone else's session\n",
1289 ZR_DEVNAME(zr
), __func__
);
1294 zr
->overlay_active
= fh
->overlay_active
= ZORAN_FREE
;
1295 zr
->v4l_overlay_active
= 0;
1296 /* When a grab is running, the video simply
1297 * won't be switched on any more */
1298 if (!zr
->v4l_memgrab_active
)
1299 zr36057_overlay(zr
, 0);
1300 zr
->overlay_mask
= NULL
;
1302 if (!zr
->vbuf_base
|| !fh
->overlay_settings
.is_set
) {
1305 "%s: %s - buffer or window not set\n",
1306 ZR_DEVNAME(zr
), __func__
);
1309 if (!fh
->overlay_settings
.format
) {
1312 "%s: %s - no overlay format set\n",
1313 ZR_DEVNAME(zr
), __func__
);
1316 zr
->overlay_active
= fh
->overlay_active
= ZORAN_LOCKED
;
1317 zr
->v4l_overlay_active
= 1;
1318 zr
->overlay_mask
= fh
->overlay_mask
;
1319 zr
->overlay_settings
= fh
->overlay_settings
;
1320 if (!zr
->v4l_memgrab_active
)
1321 zr36057_overlay(zr
, 1);
1322 /* When a grab is running, the video will be
1323 * switched on when grab is finished */
1326 /* Make sure the changes come into effect */
1327 return wait_grab_pending(zr
);
1330 /* get the status of a buffer in the clients buffer queue */
1331 static int zoran_v4l2_buffer_status(struct zoran_fh
*fh
,
1332 struct v4l2_buffer
*buf
, int num
)
1334 struct zoran
*zr
= fh
->zr
;
1335 unsigned long flags
;
1337 buf
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1339 switch (fh
->map_mode
) {
1340 case ZORAN_MAP_MODE_RAW
:
1342 if (num
< 0 || num
>= fh
->buffers
.num_buffers
||
1343 !fh
->buffers
.allocated
) {
1346 "%s: %s - wrong number or buffers not allocated\n",
1347 ZR_DEVNAME(zr
), __func__
);
1351 spin_lock_irqsave(&zr
->spinlock
, flags
);
1354 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1355 ZR_DEVNAME(zr
), __func__
,
1356 "FAL"[fh
->buffers
.active
], num
,
1357 "UPMD"[zr
->v4l_buffers
.buffer
[num
].state
],
1358 fh
->buffers
.buffer
[num
].map
? 'Y' : 'N');
1359 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
1361 buf
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1362 buf
->length
= fh
->buffers
.buffer_size
;
1365 buf
->bytesused
= fh
->buffers
.buffer
[num
].bs
.length
;
1366 if (fh
->buffers
.buffer
[num
].state
== BUZ_STATE_DONE
||
1367 fh
->buffers
.buffer
[num
].state
== BUZ_STATE_USER
) {
1368 buf
->sequence
= fh
->buffers
.buffer
[num
].bs
.seq
;
1369 buf
->flags
|= V4L2_BUF_FLAG_DONE
;
1370 buf
->timestamp
= fh
->buffers
.buffer
[num
].bs
.timestamp
;
1372 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1375 if (fh
->v4l_settings
.height
<= BUZ_MAX_HEIGHT
/ 2)
1376 buf
->field
= V4L2_FIELD_TOP
;
1378 buf
->field
= V4L2_FIELD_INTERLACED
;
1382 case ZORAN_MAP_MODE_JPG_REC
:
1383 case ZORAN_MAP_MODE_JPG_PLAY
:
1386 if (num
< 0 || num
>= fh
->buffers
.num_buffers
||
1387 !fh
->buffers
.allocated
) {
1390 "%s: %s - wrong number or buffers not allocated\n",
1391 ZR_DEVNAME(zr
), __func__
);
1395 buf
->type
= (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
) ?
1396 V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1397 V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1398 buf
->length
= fh
->buffers
.buffer_size
;
1400 /* these variables are only written after frame has been captured */
1401 if (fh
->buffers
.buffer
[num
].state
== BUZ_STATE_DONE
||
1402 fh
->buffers
.buffer
[num
].state
== BUZ_STATE_USER
) {
1403 buf
->sequence
= fh
->buffers
.buffer
[num
].bs
.seq
;
1404 buf
->timestamp
= fh
->buffers
.buffer
[num
].bs
.timestamp
;
1405 buf
->bytesused
= fh
->buffers
.buffer
[num
].bs
.length
;
1406 buf
->flags
|= V4L2_BUF_FLAG_DONE
;
1408 buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
1411 /* which fields are these? */
1412 if (fh
->jpg_settings
.TmpDcm
!= 1)
1413 buf
->field
= fh
->jpg_settings
.odd_even
?
1414 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
;
1416 buf
->field
= fh
->jpg_settings
.odd_even
?
1417 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
;
1425 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1426 ZR_DEVNAME(zr
), __func__
, buf
->type
, fh
->map_mode
);
1430 buf
->memory
= V4L2_MEMORY_MMAP
;
1432 buf
->m
.offset
= buf
->length
* num
;
1438 zoran_set_norm (struct zoran
*zr
,
1443 if (zr
->v4l_buffers
.active
!= ZORAN_FREE
||
1444 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
1447 "%s: %s called while in playback/capture mode\n",
1448 ZR_DEVNAME(zr
), __func__
);
1452 if (!(norm
& zr
->card
.norms
)) {
1454 KERN_ERR
"%s: %s - unsupported norm %llx\n",
1455 ZR_DEVNAME(zr
), __func__
, norm
);
1459 if (norm
== V4L2_STD_ALL
) {
1460 unsigned int status
= 0;
1461 v4l2_std_id std
= 0;
1463 decoder_call(zr
, video
, querystd
, &std
);
1464 decoder_call(zr
, core
, s_std
, std
);
1466 /* let changes come into effect */
1469 decoder_call(zr
, video
, g_input_status
, &status
);
1470 if (status
& V4L2_IN_ST_NO_SIGNAL
) {
1473 "%s: %s - no norm detected\n",
1474 ZR_DEVNAME(zr
), __func__
);
1476 decoder_call(zr
, core
, s_std
, zr
->norm
);
1482 if (norm
& V4L2_STD_SECAM
)
1483 zr
->timing
= zr
->card
.tvn
[2];
1484 else if (norm
& V4L2_STD_NTSC
)
1485 zr
->timing
= zr
->card
.tvn
[1];
1487 zr
->timing
= zr
->card
.tvn
[0];
1489 /* We switch overlay off and on since a change in the
1490 * norm needs different VFE settings */
1491 on
= zr
->overlay_active
&& !zr
->v4l_memgrab_active
;
1493 zr36057_overlay(zr
, 0);
1495 decoder_call(zr
, core
, s_std
, norm
);
1496 encoder_call(zr
, video
, s_std_output
, norm
);
1499 zr36057_overlay(zr
, 1);
1501 /* Make sure the changes come into effect */
1508 zoran_set_input (struct zoran
*zr
,
1511 if (input
== zr
->input
) {
1515 if (zr
->v4l_buffers
.active
!= ZORAN_FREE
||
1516 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
1519 "%s: %s called while in playback/capture mode\n",
1520 ZR_DEVNAME(zr
), __func__
);
1524 if (input
< 0 || input
>= zr
->card
.inputs
) {
1527 "%s: %s - unnsupported input %d\n",
1528 ZR_DEVNAME(zr
), __func__
, input
);
1534 decoder_call(zr
, video
, s_routing
,
1535 zr
->card
.input
[input
].muxsel
, 0, 0);
1544 static int zoran_querycap(struct file
*file
, void *__fh
, struct v4l2_capability
*cap
)
1546 struct zoran_fh
*fh
= __fh
;
1547 struct zoran
*zr
= fh
->zr
;
1549 memset(cap
, 0, sizeof(*cap
));
1550 strncpy(cap
->card
, ZR_DEVNAME(zr
), sizeof(cap
->card
)-1);
1551 strncpy(cap
->driver
, "zoran", sizeof(cap
->driver
)-1);
1552 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "PCI:%s",
1553 pci_name(zr
->pci_dev
));
1554 cap
->capabilities
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE
|
1555 V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_VIDEO_OVERLAY
;
1559 static int zoran_enum_fmt(struct zoran
*zr
, struct v4l2_fmtdesc
*fmt
, int flag
)
1561 unsigned int num
, i
;
1563 for (num
= i
= 0; i
< NUM_FORMATS
; i
++) {
1564 if (zoran_formats
[i
].flags
& flag
&& num
++ == fmt
->index
) {
1565 strncpy(fmt
->description
, zoran_formats
[i
].name
,
1566 sizeof(fmt
->description
) - 1);
1567 /* fmt struct pre-zeroed, so adding '\0' not needed */
1568 fmt
->pixelformat
= zoran_formats
[i
].fourcc
;
1569 if (zoran_formats
[i
].flags
& ZORAN_FORMAT_COMPRESSED
)
1570 fmt
->flags
|= V4L2_FMT_FLAG_COMPRESSED
;
1577 static int zoran_enum_fmt_vid_cap(struct file
*file
, void *__fh
,
1578 struct v4l2_fmtdesc
*f
)
1580 struct zoran_fh
*fh
= __fh
;
1581 struct zoran
*zr
= fh
->zr
;
1583 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_CAPTURE
);
1586 static int zoran_enum_fmt_vid_out(struct file
*file
, void *__fh
,
1587 struct v4l2_fmtdesc
*f
)
1589 struct zoran_fh
*fh
= __fh
;
1590 struct zoran
*zr
= fh
->zr
;
1592 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_PLAYBACK
);
1595 static int zoran_enum_fmt_vid_overlay(struct file
*file
, void *__fh
,
1596 struct v4l2_fmtdesc
*f
)
1598 struct zoran_fh
*fh
= __fh
;
1599 struct zoran
*zr
= fh
->zr
;
1601 return zoran_enum_fmt(zr
, f
, ZORAN_FORMAT_OVERLAY
);
1604 static int zoran_g_fmt_vid_out(struct file
*file
, void *__fh
,
1605 struct v4l2_format
*fmt
)
1607 struct zoran_fh
*fh
= __fh
;
1608 struct zoran
*zr
= fh
->zr
;
1610 mutex_lock(&zr
->resource_lock
);
1612 fmt
->fmt
.pix
.width
= fh
->jpg_settings
.img_width
/ fh
->jpg_settings
.HorDcm
;
1613 fmt
->fmt
.pix
.height
= fh
->jpg_settings
.img_height
* 2 /
1614 (fh
->jpg_settings
.VerDcm
* fh
->jpg_settings
.TmpDcm
);
1615 fmt
->fmt
.pix
.sizeimage
= zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
1616 fmt
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MJPEG
;
1617 if (fh
->jpg_settings
.TmpDcm
== 1)
1618 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1619 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
);
1621 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1622 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
1623 fmt
->fmt
.pix
.bytesperline
= 0;
1624 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1626 mutex_unlock(&zr
->resource_lock
);
1630 static int zoran_g_fmt_vid_cap(struct file
*file
, void *__fh
,
1631 struct v4l2_format
*fmt
)
1633 struct zoran_fh
*fh
= __fh
;
1634 struct zoran
*zr
= fh
->zr
;
1636 if (fh
->map_mode
!= ZORAN_MAP_MODE_RAW
)
1637 return zoran_g_fmt_vid_out(file
, fh
, fmt
);
1639 mutex_lock(&zr
->resource_lock
);
1640 fmt
->fmt
.pix
.width
= fh
->v4l_settings
.width
;
1641 fmt
->fmt
.pix
.height
= fh
->v4l_settings
.height
;
1642 fmt
->fmt
.pix
.sizeimage
= fh
->v4l_settings
.bytesperline
*
1643 fh
->v4l_settings
.height
;
1644 fmt
->fmt
.pix
.pixelformat
= fh
->v4l_settings
.format
->fourcc
;
1645 fmt
->fmt
.pix
.colorspace
= fh
->v4l_settings
.format
->colorspace
;
1646 fmt
->fmt
.pix
.bytesperline
= fh
->v4l_settings
.bytesperline
;
1647 if (BUZ_MAX_HEIGHT
< (fh
->v4l_settings
.height
* 2))
1648 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1650 fmt
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
1651 mutex_unlock(&zr
->resource_lock
);
1655 static int zoran_g_fmt_vid_overlay(struct file
*file
, void *__fh
,
1656 struct v4l2_format
*fmt
)
1658 struct zoran_fh
*fh
= __fh
;
1659 struct zoran
*zr
= fh
->zr
;
1661 mutex_lock(&zr
->resource_lock
);
1663 fmt
->fmt
.win
.w
.left
= fh
->overlay_settings
.x
;
1664 fmt
->fmt
.win
.w
.top
= fh
->overlay_settings
.y
;
1665 fmt
->fmt
.win
.w
.width
= fh
->overlay_settings
.width
;
1666 fmt
->fmt
.win
.w
.height
= fh
->overlay_settings
.height
;
1667 if (fh
->overlay_settings
.width
* 2 > BUZ_MAX_HEIGHT
)
1668 fmt
->fmt
.win
.field
= V4L2_FIELD_INTERLACED
;
1670 fmt
->fmt
.win
.field
= V4L2_FIELD_TOP
;
1672 mutex_unlock(&zr
->resource_lock
);
1676 static int zoran_try_fmt_vid_overlay(struct file
*file
, void *__fh
,
1677 struct v4l2_format
*fmt
)
1679 struct zoran_fh
*fh
= __fh
;
1680 struct zoran
*zr
= fh
->zr
;
1682 mutex_lock(&zr
->resource_lock
);
1684 if (fmt
->fmt
.win
.w
.width
> BUZ_MAX_WIDTH
)
1685 fmt
->fmt
.win
.w
.width
= BUZ_MAX_WIDTH
;
1686 if (fmt
->fmt
.win
.w
.width
< BUZ_MIN_WIDTH
)
1687 fmt
->fmt
.win
.w
.width
= BUZ_MIN_WIDTH
;
1688 if (fmt
->fmt
.win
.w
.height
> BUZ_MAX_HEIGHT
)
1689 fmt
->fmt
.win
.w
.height
= BUZ_MAX_HEIGHT
;
1690 if (fmt
->fmt
.win
.w
.height
< BUZ_MIN_HEIGHT
)
1691 fmt
->fmt
.win
.w
.height
= BUZ_MIN_HEIGHT
;
1693 mutex_unlock(&zr
->resource_lock
);
1697 static int zoran_try_fmt_vid_out(struct file
*file
, void *__fh
,
1698 struct v4l2_format
*fmt
)
1700 struct zoran_fh
*fh
= __fh
;
1701 struct zoran
*zr
= fh
->zr
;
1702 struct zoran_jpg_settings settings
;
1705 if (fmt
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
)
1708 mutex_lock(&zr
->resource_lock
);
1709 settings
= fh
->jpg_settings
;
1711 /* we actually need to set 'real' parameters now */
1712 if ((fmt
->fmt
.pix
.height
* 2) > BUZ_MAX_HEIGHT
)
1713 settings
.TmpDcm
= 1;
1715 settings
.TmpDcm
= 2;
1716 settings
.decimation
= 0;
1717 if (fmt
->fmt
.pix
.height
<= fh
->jpg_settings
.img_height
/ 2)
1718 settings
.VerDcm
= 2;
1720 settings
.VerDcm
= 1;
1721 if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 4)
1722 settings
.HorDcm
= 4;
1723 else if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 2)
1724 settings
.HorDcm
= 2;
1726 settings
.HorDcm
= 1;
1727 if (settings
.TmpDcm
== 1)
1728 settings
.field_per_buff
= 2;
1730 settings
.field_per_buff
= 1;
1732 if (settings
.HorDcm
> 1) {
1733 settings
.img_x
= (BUZ_MAX_WIDTH
== 720) ? 8 : 0;
1734 settings
.img_width
= (BUZ_MAX_WIDTH
== 720) ? 704 : BUZ_MAX_WIDTH
;
1737 settings
.img_width
= BUZ_MAX_WIDTH
;
1741 res
= zoran_check_jpg_settings(zr
, &settings
, 1);
1743 goto tryfmt_unlock_and_return
;
1745 /* tell the user what we actually did */
1746 fmt
->fmt
.pix
.width
= settings
.img_width
/ settings
.HorDcm
;
1747 fmt
->fmt
.pix
.height
= settings
.img_height
* 2 /
1748 (settings
.TmpDcm
* settings
.VerDcm
);
1749 if (settings
.TmpDcm
== 1)
1750 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1751 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
);
1753 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1754 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
1756 fmt
->fmt
.pix
.sizeimage
= zoran_v4l2_calc_bufsize(&settings
);
1757 fmt
->fmt
.pix
.bytesperline
= 0;
1758 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1759 tryfmt_unlock_and_return
:
1760 mutex_unlock(&zr
->resource_lock
);
1764 static int zoran_try_fmt_vid_cap(struct file
*file
, void *__fh
,
1765 struct v4l2_format
*fmt
)
1767 struct zoran_fh
*fh
= __fh
;
1768 struct zoran
*zr
= fh
->zr
;
1772 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_MJPEG
)
1773 return zoran_try_fmt_vid_out(file
, fh
, fmt
);
1775 mutex_lock(&zr
->resource_lock
);
1777 for (i
= 0; i
< NUM_FORMATS
; i
++)
1778 if (zoran_formats
[i
].fourcc
== fmt
->fmt
.pix
.pixelformat
)
1781 if (i
== NUM_FORMATS
) {
1782 mutex_unlock(&zr
->resource_lock
);
1786 bpp
= DIV_ROUND_UP(zoran_formats
[i
].depth
, 8);
1787 v4l_bound_align_image(
1788 &fmt
->fmt
.pix
.width
, BUZ_MIN_WIDTH
, BUZ_MAX_WIDTH
, bpp
== 2 ? 1 : 2,
1789 &fmt
->fmt
.pix
.height
, BUZ_MIN_HEIGHT
, BUZ_MAX_HEIGHT
, 0, 0);
1790 mutex_unlock(&zr
->resource_lock
);
1795 static int zoran_s_fmt_vid_overlay(struct file
*file
, void *__fh
,
1796 struct v4l2_format
*fmt
)
1798 struct zoran_fh
*fh
= __fh
;
1799 struct zoran
*zr
= fh
->zr
;
1802 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
1803 fmt
->fmt
.win
.w
.left
, fmt
->fmt
.win
.w
.top
,
1804 fmt
->fmt
.win
.w
.width
,
1805 fmt
->fmt
.win
.w
.height
,
1806 fmt
->fmt
.win
.clipcount
,
1807 fmt
->fmt
.win
.bitmap
);
1808 mutex_lock(&zr
->resource_lock
);
1809 res
= setup_window(fh
, fmt
->fmt
.win
.w
.left
, fmt
->fmt
.win
.w
.top
,
1810 fmt
->fmt
.win
.w
.width
, fmt
->fmt
.win
.w
.height
,
1811 (struct v4l2_clip __user
*)fmt
->fmt
.win
.clips
,
1812 fmt
->fmt
.win
.clipcount
, fmt
->fmt
.win
.bitmap
);
1813 mutex_unlock(&zr
->resource_lock
);
1817 static int zoran_s_fmt_vid_out(struct file
*file
, void *__fh
,
1818 struct v4l2_format
*fmt
)
1820 struct zoran_fh
*fh
= __fh
;
1821 struct zoran
*zr
= fh
->zr
;
1822 __le32 printformat
= __cpu_to_le32(fmt
->fmt
.pix
.pixelformat
);
1823 struct zoran_jpg_settings settings
;
1826 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
1827 fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
,
1828 fmt
->fmt
.pix
.pixelformat
,
1829 (char *) &printformat
);
1830 if (fmt
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
)
1833 mutex_lock(&zr
->resource_lock
);
1835 if (fh
->buffers
.allocated
) {
1836 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - cannot change capture mode\n",
1839 goto sfmtjpg_unlock_and_return
;
1842 settings
= fh
->jpg_settings
;
1844 /* we actually need to set 'real' parameters now */
1845 if (fmt
->fmt
.pix
.height
* 2 > BUZ_MAX_HEIGHT
)
1846 settings
.TmpDcm
= 1;
1848 settings
.TmpDcm
= 2;
1849 settings
.decimation
= 0;
1850 if (fmt
->fmt
.pix
.height
<= fh
->jpg_settings
.img_height
/ 2)
1851 settings
.VerDcm
= 2;
1853 settings
.VerDcm
= 1;
1854 if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 4)
1855 settings
.HorDcm
= 4;
1856 else if (fmt
->fmt
.pix
.width
<= fh
->jpg_settings
.img_width
/ 2)
1857 settings
.HorDcm
= 2;
1859 settings
.HorDcm
= 1;
1860 if (settings
.TmpDcm
== 1)
1861 settings
.field_per_buff
= 2;
1863 settings
.field_per_buff
= 1;
1865 if (settings
.HorDcm
> 1) {
1866 settings
.img_x
= (BUZ_MAX_WIDTH
== 720) ? 8 : 0;
1867 settings
.img_width
= (BUZ_MAX_WIDTH
== 720) ? 704 : BUZ_MAX_WIDTH
;
1870 settings
.img_width
= BUZ_MAX_WIDTH
;
1874 res
= zoran_check_jpg_settings(zr
, &settings
, 0);
1876 goto sfmtjpg_unlock_and_return
;
1878 /* it's ok, so set them */
1879 fh
->jpg_settings
= settings
;
1881 map_mode_jpg(fh
, fmt
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
);
1882 fh
->buffers
.buffer_size
= zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
1884 /* tell the user what we actually did */
1885 fmt
->fmt
.pix
.width
= settings
.img_width
/ settings
.HorDcm
;
1886 fmt
->fmt
.pix
.height
= settings
.img_height
* 2 /
1887 (settings
.TmpDcm
* settings
.VerDcm
);
1888 if (settings
.TmpDcm
== 1)
1889 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1890 V4L2_FIELD_SEQ_TB
: V4L2_FIELD_SEQ_BT
);
1892 fmt
->fmt
.pix
.field
= (fh
->jpg_settings
.odd_even
?
1893 V4L2_FIELD_TOP
: V4L2_FIELD_BOTTOM
);
1894 fmt
->fmt
.pix
.bytesperline
= 0;
1895 fmt
->fmt
.pix
.sizeimage
= fh
->buffers
.buffer_size
;
1896 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1898 sfmtjpg_unlock_and_return
:
1899 mutex_unlock(&zr
->resource_lock
);
1903 static int zoran_s_fmt_vid_cap(struct file
*file
, void *__fh
,
1904 struct v4l2_format
*fmt
)
1906 struct zoran_fh
*fh
= __fh
;
1907 struct zoran
*zr
= fh
->zr
;
1911 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_MJPEG
)
1912 return zoran_s_fmt_vid_out(file
, fh
, fmt
);
1914 for (i
= 0; i
< NUM_FORMATS
; i
++)
1915 if (fmt
->fmt
.pix
.pixelformat
== zoran_formats
[i
].fourcc
)
1917 if (i
== NUM_FORMATS
) {
1918 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
1919 ZR_DEVNAME(zr
), fmt
->fmt
.pix
.pixelformat
);
1923 mutex_lock(&zr
->resource_lock
);
1925 if ((fh
->map_mode
!= ZORAN_MAP_MODE_RAW
&& fh
->buffers
.allocated
) ||
1926 fh
->buffers
.active
!= ZORAN_FREE
) {
1927 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FMT - cannot change capture mode\n",
1930 goto sfmtv4l_unlock_and_return
;
1932 if (fmt
->fmt
.pix
.height
> BUZ_MAX_HEIGHT
)
1933 fmt
->fmt
.pix
.height
= BUZ_MAX_HEIGHT
;
1934 if (fmt
->fmt
.pix
.width
> BUZ_MAX_WIDTH
)
1935 fmt
->fmt
.pix
.width
= BUZ_MAX_WIDTH
;
1939 res
= zoran_v4l_set_format(fh
, fmt
->fmt
.pix
.width
, fmt
->fmt
.pix
.height
,
1942 goto sfmtv4l_unlock_and_return
;
1944 /* tell the user the results/missing stuff */
1945 fmt
->fmt
.pix
.bytesperline
= fh
->v4l_settings
.bytesperline
;
1946 fmt
->fmt
.pix
.sizeimage
= fh
->v4l_settings
.height
* fh
->v4l_settings
.bytesperline
;
1947 fmt
->fmt
.pix
.colorspace
= fh
->v4l_settings
.format
->colorspace
;
1948 if (BUZ_MAX_HEIGHT
< (fh
->v4l_settings
.height
* 2))
1949 fmt
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1951 fmt
->fmt
.pix
.field
= V4L2_FIELD_TOP
;
1953 sfmtv4l_unlock_and_return
:
1954 mutex_unlock(&zr
->resource_lock
);
1958 static int zoran_g_fbuf(struct file
*file
, void *__fh
,
1959 struct v4l2_framebuffer
*fb
)
1961 struct zoran_fh
*fh
= __fh
;
1962 struct zoran
*zr
= fh
->zr
;
1964 memset(fb
, 0, sizeof(*fb
));
1965 mutex_lock(&zr
->resource_lock
);
1966 fb
->base
= zr
->vbuf_base
;
1967 fb
->fmt
.width
= zr
->vbuf_width
;
1968 fb
->fmt
.height
= zr
->vbuf_height
;
1969 if (zr
->overlay_settings
.format
)
1970 fb
->fmt
.pixelformat
= fh
->overlay_settings
.format
->fourcc
;
1971 fb
->fmt
.bytesperline
= zr
->vbuf_bytesperline
;
1972 mutex_unlock(&zr
->resource_lock
);
1973 fb
->fmt
.colorspace
= V4L2_COLORSPACE_SRGB
;
1974 fb
->fmt
.field
= V4L2_FIELD_INTERLACED
;
1975 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
1980 static int zoran_s_fbuf(struct file
*file
, void *__fh
,
1981 const struct v4l2_framebuffer
*fb
)
1983 struct zoran_fh
*fh
= __fh
;
1984 struct zoran
*zr
= fh
->zr
;
1986 __le32 printformat
= __cpu_to_le32(fb
->fmt
.pixelformat
);
1988 for (i
= 0; i
< NUM_FORMATS
; i
++)
1989 if (zoran_formats
[i
].fourcc
== fb
->fmt
.pixelformat
)
1991 if (i
== NUM_FORMATS
) {
1992 dprintk(1, KERN_ERR
"%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
1993 ZR_DEVNAME(zr
), fb
->fmt
.pixelformat
,
1994 (char *)&printformat
);
1998 mutex_lock(&zr
->resource_lock
);
1999 res
= setup_fbuffer(fh
, fb
->base
, &zoran_formats
[i
], fb
->fmt
.width
,
2000 fb
->fmt
.height
, fb
->fmt
.bytesperline
);
2001 mutex_unlock(&zr
->resource_lock
);
2006 static int zoran_overlay(struct file
*file
, void *__fh
, unsigned int on
)
2008 struct zoran_fh
*fh
= __fh
;
2009 struct zoran
*zr
= fh
->zr
;
2012 mutex_lock(&zr
->resource_lock
);
2013 res
= setup_overlay(fh
, on
);
2014 mutex_unlock(&zr
->resource_lock
);
2019 static int zoran_streamoff(struct file
*file
, void *__fh
, enum v4l2_buf_type type
);
2021 static int zoran_reqbufs(struct file
*file
, void *__fh
, struct v4l2_requestbuffers
*req
)
2023 struct zoran_fh
*fh
= __fh
;
2024 struct zoran
*zr
= fh
->zr
;
2027 if (req
->memory
!= V4L2_MEMORY_MMAP
) {
2030 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2031 ZR_DEVNAME(zr
), req
->memory
);
2035 if (req
->count
== 0)
2036 return zoran_streamoff(file
, fh
, req
->type
);
2038 mutex_lock(&zr
->resource_lock
);
2039 if (fh
->buffers
.allocated
) {
2042 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
2045 goto v4l2reqbuf_unlock_and_return
;
2048 if (fh
->map_mode
== ZORAN_MAP_MODE_RAW
&&
2049 req
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2050 /* control user input */
2053 if (req
->count
> v4l_nbufs
)
2054 req
->count
= v4l_nbufs
;
2056 /* The next mmap will map the V4L buffers */
2058 fh
->buffers
.num_buffers
= req
->count
;
2060 if (v4l_fbuffer_alloc(fh
)) {
2062 goto v4l2reqbuf_unlock_and_return
;
2064 } else if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
||
2065 fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
) {
2066 /* we need to calculate size ourselves now */
2069 if (req
->count
> jpg_nbufs
)
2070 req
->count
= jpg_nbufs
;
2072 /* The next mmap will map the MJPEG buffers */
2073 map_mode_jpg(fh
, req
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
);
2074 fh
->buffers
.num_buffers
= req
->count
;
2075 fh
->buffers
.buffer_size
= zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
2077 if (jpg_fbuffer_alloc(fh
)) {
2079 goto v4l2reqbuf_unlock_and_return
;
2084 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2085 ZR_DEVNAME(zr
), req
->type
);
2087 goto v4l2reqbuf_unlock_and_return
;
2089 v4l2reqbuf_unlock_and_return
:
2090 mutex_unlock(&zr
->resource_lock
);
2095 static int zoran_querybuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
2097 struct zoran_fh
*fh
= __fh
;
2098 struct zoran
*zr
= fh
->zr
;
2101 mutex_lock(&zr
->resource_lock
);
2102 res
= zoran_v4l2_buffer_status(fh
, buf
, buf
->index
);
2103 mutex_unlock(&zr
->resource_lock
);
2108 static int zoran_qbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
2110 struct zoran_fh
*fh
= __fh
;
2111 struct zoran
*zr
= fh
->zr
;
2112 int res
= 0, codec_mode
, buf_type
;
2114 mutex_lock(&zr
->resource_lock
);
2116 switch (fh
->map_mode
) {
2117 case ZORAN_MAP_MODE_RAW
:
2118 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2120 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2121 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
2123 goto qbuf_unlock_and_return
;
2126 res
= zoran_v4l_queue_frame(fh
, buf
->index
);
2128 goto qbuf_unlock_and_return
;
2129 if (!zr
->v4l_memgrab_active
&& fh
->buffers
.active
== ZORAN_LOCKED
)
2130 zr36057_set_memgrab(zr
, 1);
2133 case ZORAN_MAP_MODE_JPG_REC
:
2134 case ZORAN_MAP_MODE_JPG_PLAY
:
2135 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
) {
2136 buf_type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2137 codec_mode
= BUZ_MODE_MOTION_DECOMPRESS
;
2139 buf_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2140 codec_mode
= BUZ_MODE_MOTION_COMPRESS
;
2143 if (buf
->type
!= buf_type
) {
2145 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2146 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
2148 goto qbuf_unlock_and_return
;
2151 res
= zoran_jpg_queue_frame(fh
, buf
->index
, codec_mode
);
2153 goto qbuf_unlock_and_return
;
2154 if (zr
->codec_mode
== BUZ_MODE_IDLE
&&
2155 fh
->buffers
.active
== ZORAN_LOCKED
)
2156 zr36057_enable_jpg(zr
, codec_mode
);
2162 "%s: VIDIOC_QBUF - unsupported type %d\n",
2163 ZR_DEVNAME(zr
), buf
->type
);
2167 qbuf_unlock_and_return
:
2168 mutex_unlock(&zr
->resource_lock
);
2173 static int zoran_dqbuf(struct file
*file
, void *__fh
, struct v4l2_buffer
*buf
)
2175 struct zoran_fh
*fh
= __fh
;
2176 struct zoran
*zr
= fh
->zr
;
2177 int res
= 0, buf_type
, num
= -1; /* compiler borks here (?) */
2179 mutex_lock(&zr
->resource_lock
);
2181 switch (fh
->map_mode
) {
2182 case ZORAN_MAP_MODE_RAW
:
2183 if (buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
2185 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2186 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
2188 goto dqbuf_unlock_and_return
;
2191 num
= zr
->v4l_pend
[zr
->v4l_sync_tail
& V4L_MASK_FRAME
];
2192 if (file
->f_flags
& O_NONBLOCK
&&
2193 zr
->v4l_buffers
.buffer
[num
].state
!= BUZ_STATE_DONE
) {
2195 goto dqbuf_unlock_and_return
;
2197 res
= v4l_sync(fh
, num
);
2199 goto dqbuf_unlock_and_return
;
2200 zr
->v4l_sync_tail
++;
2201 res
= zoran_v4l2_buffer_status(fh
, buf
, num
);
2204 case ZORAN_MAP_MODE_JPG_REC
:
2205 case ZORAN_MAP_MODE_JPG_PLAY
:
2207 struct zoran_sync bs
;
2209 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_PLAY
)
2210 buf_type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
2212 buf_type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2214 if (buf
->type
!= buf_type
) {
2216 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2217 ZR_DEVNAME(zr
), buf
->type
, fh
->map_mode
);
2219 goto dqbuf_unlock_and_return
;
2222 num
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
2224 if (file
->f_flags
& O_NONBLOCK
&&
2225 zr
->jpg_buffers
.buffer
[num
].state
!= BUZ_STATE_DONE
) {
2227 goto dqbuf_unlock_and_return
;
2229 bs
.frame
= 0; /* suppress compiler warning */
2230 res
= jpg_sync(fh
, &bs
);
2232 goto dqbuf_unlock_and_return
;
2233 res
= zoran_v4l2_buffer_status(fh
, buf
, bs
.frame
);
2239 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2240 ZR_DEVNAME(zr
), buf
->type
);
2244 dqbuf_unlock_and_return
:
2245 mutex_unlock(&zr
->resource_lock
);
2250 static int zoran_streamon(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
2252 struct zoran_fh
*fh
= __fh
;
2253 struct zoran
*zr
= fh
->zr
;
2256 mutex_lock(&zr
->resource_lock
);
2258 switch (fh
->map_mode
) {
2259 case ZORAN_MAP_MODE_RAW
: /* raw capture */
2260 if (zr
->v4l_buffers
.active
!= ZORAN_ACTIVE
||
2261 fh
->buffers
.active
!= ZORAN_ACTIVE
) {
2263 goto strmon_unlock_and_return
;
2266 zr
->v4l_buffers
.active
= fh
->buffers
.active
= ZORAN_LOCKED
;
2267 zr
->v4l_settings
= fh
->v4l_settings
;
2269 zr
->v4l_sync_tail
= zr
->v4l_pend_tail
;
2270 if (!zr
->v4l_memgrab_active
&&
2271 zr
->v4l_pend_head
!= zr
->v4l_pend_tail
) {
2272 zr36057_set_memgrab(zr
, 1);
2276 case ZORAN_MAP_MODE_JPG_REC
:
2277 case ZORAN_MAP_MODE_JPG_PLAY
:
2278 /* what is the codec mode right now? */
2279 if (zr
->jpg_buffers
.active
!= ZORAN_ACTIVE
||
2280 fh
->buffers
.active
!= ZORAN_ACTIVE
) {
2282 goto strmon_unlock_and_return
;
2285 zr
->jpg_buffers
.active
= fh
->buffers
.active
= ZORAN_LOCKED
;
2287 if (zr
->jpg_que_head
!= zr
->jpg_que_tail
) {
2288 /* Start the jpeg codec when the first frame is queued */
2296 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2297 ZR_DEVNAME(zr
), fh
->map_mode
);
2301 strmon_unlock_and_return
:
2302 mutex_unlock(&zr
->resource_lock
);
2307 static int zoran_streamoff(struct file
*file
, void *__fh
, enum v4l2_buf_type type
)
2309 struct zoran_fh
*fh
= __fh
;
2310 struct zoran
*zr
= fh
->zr
;
2312 unsigned long flags
;
2314 mutex_lock(&zr
->resource_lock
);
2316 switch (fh
->map_mode
) {
2317 case ZORAN_MAP_MODE_RAW
: /* raw capture */
2318 if (fh
->buffers
.active
== ZORAN_FREE
&&
2319 zr
->v4l_buffers
.active
!= ZORAN_FREE
) {
2320 res
= -EPERM
; /* stay off other's settings! */
2321 goto strmoff_unlock_and_return
;
2323 if (zr
->v4l_buffers
.active
== ZORAN_FREE
)
2324 goto strmoff_unlock_and_return
;
2326 spin_lock_irqsave(&zr
->spinlock
, flags
);
2327 /* unload capture */
2328 if (zr
->v4l_memgrab_active
) {
2330 zr36057_set_memgrab(zr
, 0);
2333 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++)
2334 zr
->v4l_buffers
.buffer
[i
].state
= BUZ_STATE_USER
;
2335 fh
->buffers
= zr
->v4l_buffers
;
2337 zr
->v4l_buffers
.active
= fh
->buffers
.active
= ZORAN_FREE
;
2339 zr
->v4l_grab_seq
= 0;
2340 zr
->v4l_pend_head
= zr
->v4l_pend_tail
= 0;
2341 zr
->v4l_sync_tail
= 0;
2343 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
2347 case ZORAN_MAP_MODE_JPG_REC
:
2348 case ZORAN_MAP_MODE_JPG_PLAY
:
2349 if (fh
->buffers
.active
== ZORAN_FREE
&&
2350 zr
->jpg_buffers
.active
!= ZORAN_FREE
) {
2351 res
= -EPERM
; /* stay off other's settings! */
2352 goto strmoff_unlock_and_return
;
2354 if (zr
->jpg_buffers
.active
== ZORAN_FREE
)
2355 goto strmoff_unlock_and_return
;
2357 res
= jpg_qbuf(fh
, -1,
2358 (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
) ?
2359 BUZ_MODE_MOTION_COMPRESS
:
2360 BUZ_MODE_MOTION_DECOMPRESS
);
2362 goto strmoff_unlock_and_return
;
2366 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2367 ZR_DEVNAME(zr
), fh
->map_mode
);
2371 strmoff_unlock_and_return
:
2372 mutex_unlock(&zr
->resource_lock
);
2377 static int zoran_queryctrl(struct file
*file
, void *__fh
,
2378 struct v4l2_queryctrl
*ctrl
)
2380 struct zoran_fh
*fh
= __fh
;
2381 struct zoran
*zr
= fh
->zr
;
2383 /* we only support hue/saturation/contrast/brightness */
2384 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
2385 ctrl
->id
> V4L2_CID_HUE
)
2388 decoder_call(zr
, core
, queryctrl
, ctrl
);
2393 static int zoran_g_ctrl(struct file
*file
, void *__fh
, struct v4l2_control
*ctrl
)
2395 struct zoran_fh
*fh
= __fh
;
2396 struct zoran
*zr
= fh
->zr
;
2398 /* we only support hue/saturation/contrast/brightness */
2399 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
2400 ctrl
->id
> V4L2_CID_HUE
)
2403 mutex_lock(&zr
->resource_lock
);
2404 decoder_call(zr
, core
, g_ctrl
, ctrl
);
2405 mutex_unlock(&zr
->resource_lock
);
2410 static int zoran_s_ctrl(struct file
*file
, void *__fh
, struct v4l2_control
*ctrl
)
2412 struct zoran_fh
*fh
= __fh
;
2413 struct zoran
*zr
= fh
->zr
;
2415 /* we only support hue/saturation/contrast/brightness */
2416 if (ctrl
->id
< V4L2_CID_BRIGHTNESS
||
2417 ctrl
->id
> V4L2_CID_HUE
)
2420 mutex_lock(&zr
->resource_lock
);
2421 decoder_call(zr
, core
, s_ctrl
, ctrl
);
2422 mutex_unlock(&zr
->resource_lock
);
2427 static int zoran_g_std(struct file
*file
, void *__fh
, v4l2_std_id
*std
)
2429 struct zoran_fh
*fh
= __fh
;
2430 struct zoran
*zr
= fh
->zr
;
2432 mutex_lock(&zr
->resource_lock
);
2434 mutex_unlock(&zr
->resource_lock
);
2438 static int zoran_s_std(struct file
*file
, void *__fh
, v4l2_std_id std
)
2440 struct zoran_fh
*fh
= __fh
;
2441 struct zoran
*zr
= fh
->zr
;
2444 mutex_lock(&zr
->resource_lock
);
2445 res
= zoran_set_norm(zr
, std
);
2447 goto sstd_unlock_and_return
;
2449 res
= wait_grab_pending(zr
);
2450 sstd_unlock_and_return
:
2451 mutex_unlock(&zr
->resource_lock
);
2455 static int zoran_enum_input(struct file
*file
, void *__fh
,
2456 struct v4l2_input
*inp
)
2458 struct zoran_fh
*fh
= __fh
;
2459 struct zoran
*zr
= fh
->zr
;
2461 if (inp
->index
>= zr
->card
.inputs
)
2464 strncpy(inp
->name
, zr
->card
.input
[inp
->index
].name
,
2465 sizeof(inp
->name
) - 1);
2466 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
2467 inp
->std
= V4L2_STD_ALL
;
2469 /* Get status of video decoder */
2470 mutex_lock(&zr
->resource_lock
);
2471 decoder_call(zr
, video
, g_input_status
, &inp
->status
);
2472 mutex_unlock(&zr
->resource_lock
);
2476 static int zoran_g_input(struct file
*file
, void *__fh
, unsigned int *input
)
2478 struct zoran_fh
*fh
= __fh
;
2479 struct zoran
*zr
= fh
->zr
;
2481 mutex_lock(&zr
->resource_lock
);
2483 mutex_unlock(&zr
->resource_lock
);
2488 static int zoran_s_input(struct file
*file
, void *__fh
, unsigned int input
)
2490 struct zoran_fh
*fh
= __fh
;
2491 struct zoran
*zr
= fh
->zr
;
2494 mutex_lock(&zr
->resource_lock
);
2495 res
= zoran_set_input(zr
, input
);
2497 goto sinput_unlock_and_return
;
2499 /* Make sure the changes come into effect */
2500 res
= wait_grab_pending(zr
);
2501 sinput_unlock_and_return
:
2502 mutex_unlock(&zr
->resource_lock
);
2506 static int zoran_enum_output(struct file
*file
, void *__fh
,
2507 struct v4l2_output
*outp
)
2509 if (outp
->index
!= 0)
2513 outp
->type
= V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY
;
2514 strncpy(outp
->name
, "Autodetect", sizeof(outp
->name
)-1);
2519 static int zoran_g_output(struct file
*file
, void *__fh
, unsigned int *output
)
2526 static int zoran_s_output(struct file
*file
, void *__fh
, unsigned int output
)
2534 /* cropping (sub-frame capture) */
2535 static int zoran_cropcap(struct file
*file
, void *__fh
,
2536 struct v4l2_cropcap
*cropcap
)
2538 struct zoran_fh
*fh
= __fh
;
2539 struct zoran
*zr
= fh
->zr
;
2540 int type
= cropcap
->type
, res
= 0;
2542 memset(cropcap
, 0, sizeof(*cropcap
));
2543 cropcap
->type
= type
;
2545 mutex_lock(&zr
->resource_lock
);
2547 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
2548 (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2549 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
2551 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2554 goto cropcap_unlock_and_return
;
2557 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
2558 cropcap
->bounds
.width
= BUZ_MAX_WIDTH
;
2559 cropcap
->bounds
.height
= BUZ_MAX_HEIGHT
;
2560 cropcap
->defrect
.top
= cropcap
->defrect
.left
= 0;
2561 cropcap
->defrect
.width
= BUZ_MIN_WIDTH
;
2562 cropcap
->defrect
.height
= BUZ_MIN_HEIGHT
;
2563 cropcap_unlock_and_return
:
2564 mutex_unlock(&zr
->resource_lock
);
2568 static int zoran_g_crop(struct file
*file
, void *__fh
, struct v4l2_crop
*crop
)
2570 struct zoran_fh
*fh
= __fh
;
2571 struct zoran
*zr
= fh
->zr
;
2572 int type
= crop
->type
, res
= 0;
2574 memset(crop
, 0, sizeof(*crop
));
2577 mutex_lock(&zr
->resource_lock
);
2579 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
2580 (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2581 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
2584 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2587 goto gcrop_unlock_and_return
;
2590 crop
->c
.top
= fh
->jpg_settings
.img_y
;
2591 crop
->c
.left
= fh
->jpg_settings
.img_x
;
2592 crop
->c
.width
= fh
->jpg_settings
.img_width
;
2593 crop
->c
.height
= fh
->jpg_settings
.img_height
;
2595 gcrop_unlock_and_return
:
2596 mutex_unlock(&zr
->resource_lock
);
2601 static int zoran_s_crop(struct file
*file
, void *__fh
, const struct v4l2_crop
*crop
)
2603 struct zoran_fh
*fh
= __fh
;
2604 struct zoran
*zr
= fh
->zr
;
2606 struct zoran_jpg_settings settings
;
2608 settings
= fh
->jpg_settings
;
2610 mutex_lock(&zr
->resource_lock
);
2612 if (fh
->buffers
.allocated
) {
2614 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2617 goto scrop_unlock_and_return
;
2620 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
2621 (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
2622 fh
->map_mode
== ZORAN_MAP_MODE_RAW
)) {
2624 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2627 goto scrop_unlock_and_return
;
2630 /* move into a form that we understand */
2631 settings
.img_x
= crop
->c
.left
;
2632 settings
.img_y
= crop
->c
.top
;
2633 settings
.img_width
= crop
->c
.width
;
2634 settings
.img_height
= crop
->c
.height
;
2636 /* check validity */
2637 res
= zoran_check_jpg_settings(zr
, &settings
, 0);
2639 goto scrop_unlock_and_return
;
2642 fh
->jpg_settings
= settings
;
2644 scrop_unlock_and_return
:
2645 mutex_unlock(&zr
->resource_lock
);
2649 static int zoran_g_jpegcomp(struct file
*file
, void *__fh
,
2650 struct v4l2_jpegcompression
*params
)
2652 struct zoran_fh
*fh
= __fh
;
2653 struct zoran
*zr
= fh
->zr
;
2654 memset(params
, 0, sizeof(*params
));
2656 mutex_lock(&zr
->resource_lock
);
2658 params
->quality
= fh
->jpg_settings
.jpg_comp
.quality
;
2659 params
->APPn
= fh
->jpg_settings
.jpg_comp
.APPn
;
2660 memcpy(params
->APP_data
,
2661 fh
->jpg_settings
.jpg_comp
.APP_data
,
2662 fh
->jpg_settings
.jpg_comp
.APP_len
);
2663 params
->APP_len
= fh
->jpg_settings
.jpg_comp
.APP_len
;
2664 memcpy(params
->COM_data
,
2665 fh
->jpg_settings
.jpg_comp
.COM_data
,
2666 fh
->jpg_settings
.jpg_comp
.COM_len
);
2667 params
->COM_len
= fh
->jpg_settings
.jpg_comp
.COM_len
;
2668 params
->jpeg_markers
=
2669 fh
->jpg_settings
.jpg_comp
.jpeg_markers
;
2671 mutex_unlock(&zr
->resource_lock
);
2676 static int zoran_s_jpegcomp(struct file
*file
, void *__fh
,
2677 const struct v4l2_jpegcompression
*params
)
2679 struct zoran_fh
*fh
= __fh
;
2680 struct zoran
*zr
= fh
->zr
;
2682 struct zoran_jpg_settings settings
;
2684 settings
= fh
->jpg_settings
;
2686 settings
.jpg_comp
= *params
;
2688 mutex_lock(&zr
->resource_lock
);
2690 if (fh
->buffers
.active
!= ZORAN_FREE
) {
2691 dprintk(1, KERN_WARNING
2692 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
2695 goto sjpegc_unlock_and_return
;
2698 res
= zoran_check_jpg_settings(zr
, &settings
, 0);
2700 goto sjpegc_unlock_and_return
;
2701 if (!fh
->buffers
.allocated
)
2702 fh
->buffers
.buffer_size
=
2703 zoran_v4l2_calc_bufsize(&fh
->jpg_settings
);
2704 fh
->jpg_settings
.jpg_comp
= settings
.jpg_comp
;
2705 sjpegc_unlock_and_return
:
2706 mutex_unlock(&zr
->resource_lock
);
2712 zoran_poll (struct file
*file
,
2715 struct zoran_fh
*fh
= file
->private_data
;
2716 struct zoran
*zr
= fh
->zr
;
2718 unsigned long flags
;
2720 /* we should check whether buffers are ready to be synced on
2721 * (w/o waits - O_NONBLOCK) here
2722 * if ready for read (sync), return POLLIN|POLLRDNORM,
2723 * if ready for write (sync), return POLLOUT|POLLWRNORM,
2724 * if error, return POLLERR,
2725 * if no buffers queued or so, return POLLNVAL
2728 mutex_lock(&zr
->resource_lock
);
2730 switch (fh
->map_mode
) {
2731 case ZORAN_MAP_MODE_RAW
:
2732 poll_wait(file
, &zr
->v4l_capq
, wait
);
2733 frame
= zr
->v4l_pend
[zr
->v4l_sync_tail
& V4L_MASK_FRAME
];
2735 spin_lock_irqsave(&zr
->spinlock
, flags
);
2738 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
2739 ZR_DEVNAME(zr
), __func__
,
2740 "FAL"[fh
->buffers
.active
], zr
->v4l_sync_tail
,
2741 "UPMD"[zr
->v4l_buffers
.buffer
[frame
].state
],
2742 zr
->v4l_pend_tail
, zr
->v4l_pend_head
);
2743 /* Process is the one capturing? */
2744 if (fh
->buffers
.active
!= ZORAN_FREE
&&
2745 /* Buffer ready to DQBUF? */
2746 zr
->v4l_buffers
.buffer
[frame
].state
== BUZ_STATE_DONE
)
2747 res
= POLLIN
| POLLRDNORM
;
2748 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
2752 case ZORAN_MAP_MODE_JPG_REC
:
2753 case ZORAN_MAP_MODE_JPG_PLAY
:
2754 poll_wait(file
, &zr
->jpg_capq
, wait
);
2755 frame
= zr
->jpg_pend
[zr
->jpg_que_tail
& BUZ_MASK_FRAME
];
2757 spin_lock_irqsave(&zr
->spinlock
, flags
);
2760 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
2761 ZR_DEVNAME(zr
), __func__
,
2762 "FAL"[fh
->buffers
.active
], zr
->jpg_que_tail
,
2763 "UPMD"[zr
->jpg_buffers
.buffer
[frame
].state
],
2764 zr
->jpg_que_head
, zr
->jpg_dma_tail
, zr
->jpg_dma_head
);
2765 if (fh
->buffers
.active
!= ZORAN_FREE
&&
2766 zr
->jpg_buffers
.buffer
[frame
].state
== BUZ_STATE_DONE
) {
2767 if (fh
->map_mode
== ZORAN_MAP_MODE_JPG_REC
)
2768 res
= POLLIN
| POLLRDNORM
;
2770 res
= POLLOUT
| POLLWRNORM
;
2772 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
2779 "%s: %s - internal error, unknown map_mode=%d\n",
2780 ZR_DEVNAME(zr
), __func__
, fh
->map_mode
);
2784 mutex_unlock(&zr
->resource_lock
);
2791 * This maps the buffers to user space.
2793 * Depending on the state of fh->map_mode
2794 * the V4L or the MJPEG buffers are mapped
2795 * per buffer or all together
2797 * Note that we need to connect to some
2798 * unmap signal event to unmap the de-allocate
2799 * the buffer accordingly (zoran_vm_close())
2803 zoran_vm_open (struct vm_area_struct
*vma
)
2805 struct zoran_mapping
*map
= vma
->vm_private_data
;
2811 zoran_vm_close (struct vm_area_struct
*vma
)
2813 struct zoran_mapping
*map
= vma
->vm_private_data
;
2814 struct zoran_fh
*fh
= map
->fh
;
2815 struct zoran
*zr
= fh
->zr
;
2818 if (--map
->count
> 0)
2821 dprintk(3, KERN_INFO
"%s: %s - munmap(%s)\n", ZR_DEVNAME(zr
),
2822 __func__
, mode_name(fh
->map_mode
));
2824 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++) {
2825 if (fh
->buffers
.buffer
[i
].map
== map
)
2826 fh
->buffers
.buffer
[i
].map
= NULL
;
2830 /* Any buffers still mapped? */
2831 for (i
= 0; i
< fh
->buffers
.num_buffers
; i
++)
2832 if (fh
->buffers
.buffer
[i
].map
)
2835 dprintk(3, KERN_INFO
"%s: %s - free %s buffers\n", ZR_DEVNAME(zr
),
2836 __func__
, mode_name(fh
->map_mode
));
2838 mutex_lock(&zr
->resource_lock
);
2840 if (fh
->map_mode
== ZORAN_MAP_MODE_RAW
) {
2841 if (fh
->buffers
.active
!= ZORAN_FREE
) {
2842 unsigned long flags
;
2844 spin_lock_irqsave(&zr
->spinlock
, flags
);
2845 zr36057_set_memgrab(zr
, 0);
2846 zr
->v4l_buffers
.allocated
= 0;
2847 zr
->v4l_buffers
.active
= fh
->buffers
.active
= ZORAN_FREE
;
2848 spin_unlock_irqrestore(&zr
->spinlock
, flags
);
2850 v4l_fbuffer_free(fh
);
2852 if (fh
->buffers
.active
!= ZORAN_FREE
) {
2853 jpg_qbuf(fh
, -1, zr
->codec_mode
);
2854 zr
->jpg_buffers
.allocated
= 0;
2855 zr
->jpg_buffers
.active
= fh
->buffers
.active
= ZORAN_FREE
;
2857 jpg_fbuffer_free(fh
);
2860 mutex_unlock(&zr
->resource_lock
);
2863 static const struct vm_operations_struct zoran_vm_ops
= {
2864 .open
= zoran_vm_open
,
2865 .close
= zoran_vm_close
,
2869 zoran_mmap (struct file
*file
,
2870 struct vm_area_struct
*vma
)
2872 struct zoran_fh
*fh
= file
->private_data
;
2873 struct zoran
*zr
= fh
->zr
;
2874 unsigned long size
= (vma
->vm_end
- vma
->vm_start
);
2875 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
2877 unsigned long page
, start
= vma
->vm_start
, todo
, pos
, fraglen
;
2879 struct zoran_mapping
*map
;
2883 KERN_INFO
"%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
2884 ZR_DEVNAME(zr
), __func__
,
2885 mode_name(fh
->map_mode
), vma
->vm_start
, vma
->vm_end
, size
);
2887 if (!(vma
->vm_flags
& VM_SHARED
) || !(vma
->vm_flags
& VM_READ
) ||
2888 !(vma
->vm_flags
& VM_WRITE
)) {
2891 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
2892 ZR_DEVNAME(zr
), __func__
);
2896 mutex_lock(&zr
->resource_lock
);
2898 if (!fh
->buffers
.allocated
) {
2901 "%s: %s(%s) - buffers not yet allocated\n",
2902 ZR_DEVNAME(zr
), __func__
, mode_name(fh
->map_mode
));
2904 goto mmap_unlock_and_return
;
2907 first
= offset
/ fh
->buffers
.buffer_size
;
2908 last
= first
- 1 + size
/ fh
->buffers
.buffer_size
;
2909 if (offset
% fh
->buffers
.buffer_size
!= 0 ||
2910 size
% fh
->buffers
.buffer_size
!= 0 || first
< 0 ||
2911 last
< 0 || first
>= fh
->buffers
.num_buffers
||
2912 last
>= fh
->buffers
.buffer_size
) {
2915 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
2916 ZR_DEVNAME(zr
), __func__
, mode_name(fh
->map_mode
), offset
, size
,
2917 fh
->buffers
.buffer_size
,
2918 fh
->buffers
.num_buffers
);
2920 goto mmap_unlock_and_return
;
2923 /* Check if any buffers are already mapped */
2924 for (i
= first
; i
<= last
; i
++) {
2925 if (fh
->buffers
.buffer
[i
].map
) {
2928 "%s: %s(%s) - buffer %d already mapped\n",
2929 ZR_DEVNAME(zr
), __func__
, mode_name(fh
->map_mode
), i
);
2931 goto mmap_unlock_and_return
;
2935 /* map these buffers */
2936 map
= kmalloc(sizeof(struct zoran_mapping
), GFP_KERNEL
);
2939 goto mmap_unlock_and_return
;
2944 vma
->vm_ops
= &zoran_vm_ops
;
2945 vma
->vm_flags
|= VM_DONTEXPAND
;
2946 vma
->vm_private_data
= map
;
2948 if (fh
->map_mode
== ZORAN_MAP_MODE_RAW
) {
2949 for (i
= first
; i
<= last
; i
++) {
2951 if (todo
> fh
->buffers
.buffer_size
)
2952 todo
= fh
->buffers
.buffer_size
;
2953 page
= fh
->buffers
.buffer
[i
].v4l
.fbuffer_phys
;
2954 if (remap_pfn_range(vma
, start
, page
>> PAGE_SHIFT
,
2955 todo
, PAGE_SHARED
)) {
2958 "%s: %s(V4L) - remap_pfn_range failed\n",
2959 ZR_DEVNAME(zr
), __func__
);
2961 goto mmap_unlock_and_return
;
2965 fh
->buffers
.buffer
[i
].map
= map
;
2970 for (i
= first
; i
<= last
; i
++) {
2972 j
< fh
->buffers
.buffer_size
/ PAGE_SIZE
;
2975 (le32_to_cpu(fh
->buffers
.buffer
[i
].jpg
.
2976 frag_tab
[2 * j
+ 1]) & ~1) << 1;
2981 le32_to_cpu(fh
->buffers
.
2982 buffer
[i
].jpg
.frag_tab
[2 * j
]);
2983 /* should just be pos on i386 */
2984 page
= virt_to_phys(bus_to_virt(pos
))
2986 if (remap_pfn_range(vma
, start
, page
,
2987 todo
, PAGE_SHARED
)) {
2990 "%s: %s(V4L) - remap_pfn_range failed\n",
2991 ZR_DEVNAME(zr
), __func__
);
2993 goto mmap_unlock_and_return
;
2999 if (le32_to_cpu(fh
->buffers
.buffer
[i
].jpg
.
3000 frag_tab
[2 * j
+ 1]) & 1)
3001 break; /* was last fragment */
3003 fh
->buffers
.buffer
[i
].map
= map
;
3010 mmap_unlock_and_return
:
3011 mutex_unlock(&zr
->resource_lock
);
3016 static const struct v4l2_ioctl_ops zoran_ioctl_ops
= {
3017 .vidioc_querycap
= zoran_querycap
,
3018 .vidioc_cropcap
= zoran_cropcap
,
3019 .vidioc_s_crop
= zoran_s_crop
,
3020 .vidioc_g_crop
= zoran_g_crop
,
3021 .vidioc_enum_input
= zoran_enum_input
,
3022 .vidioc_g_input
= zoran_g_input
,
3023 .vidioc_s_input
= zoran_s_input
,
3024 .vidioc_enum_output
= zoran_enum_output
,
3025 .vidioc_g_output
= zoran_g_output
,
3026 .vidioc_s_output
= zoran_s_output
,
3027 .vidioc_g_fbuf
= zoran_g_fbuf
,
3028 .vidioc_s_fbuf
= zoran_s_fbuf
,
3029 .vidioc_g_std
= zoran_g_std
,
3030 .vidioc_s_std
= zoran_s_std
,
3031 .vidioc_g_jpegcomp
= zoran_g_jpegcomp
,
3032 .vidioc_s_jpegcomp
= zoran_s_jpegcomp
,
3033 .vidioc_overlay
= zoran_overlay
,
3034 .vidioc_reqbufs
= zoran_reqbufs
,
3035 .vidioc_querybuf
= zoran_querybuf
,
3036 .vidioc_qbuf
= zoran_qbuf
,
3037 .vidioc_dqbuf
= zoran_dqbuf
,
3038 .vidioc_streamon
= zoran_streamon
,
3039 .vidioc_streamoff
= zoran_streamoff
,
3040 .vidioc_enum_fmt_vid_cap
= zoran_enum_fmt_vid_cap
,
3041 .vidioc_enum_fmt_vid_out
= zoran_enum_fmt_vid_out
,
3042 .vidioc_enum_fmt_vid_overlay
= zoran_enum_fmt_vid_overlay
,
3043 .vidioc_g_fmt_vid_cap
= zoran_g_fmt_vid_cap
,
3044 .vidioc_g_fmt_vid_out
= zoran_g_fmt_vid_out
,
3045 .vidioc_g_fmt_vid_overlay
= zoran_g_fmt_vid_overlay
,
3046 .vidioc_s_fmt_vid_cap
= zoran_s_fmt_vid_cap
,
3047 .vidioc_s_fmt_vid_out
= zoran_s_fmt_vid_out
,
3048 .vidioc_s_fmt_vid_overlay
= zoran_s_fmt_vid_overlay
,
3049 .vidioc_try_fmt_vid_cap
= zoran_try_fmt_vid_cap
,
3050 .vidioc_try_fmt_vid_out
= zoran_try_fmt_vid_out
,
3051 .vidioc_try_fmt_vid_overlay
= zoran_try_fmt_vid_overlay
,
3052 .vidioc_queryctrl
= zoran_queryctrl
,
3053 .vidioc_s_ctrl
= zoran_s_ctrl
,
3054 .vidioc_g_ctrl
= zoran_g_ctrl
,
3057 /* please use zr->resource_lock consistently and kill this wrapper */
3058 static long zoran_ioctl(struct file
*file
, unsigned int cmd
,
3061 struct zoran_fh
*fh
= file
->private_data
;
3062 struct zoran
*zr
= fh
->zr
;
3065 mutex_lock(&zr
->other_lock
);
3066 ret
= video_ioctl2(file
, cmd
, arg
);
3067 mutex_unlock(&zr
->other_lock
);
3072 static const struct v4l2_file_operations zoran_fops
= {
3073 .owner
= THIS_MODULE
,
3075 .release
= zoran_close
,
3076 .unlocked_ioctl
= zoran_ioctl
,
3078 .write
= zoran_write
,
3083 struct video_device zoran_template
= {
3085 .fops
= &zoran_fops
,
3086 .ioctl_ops
= &zoran_ioctl_ops
,
3087 .release
= &zoran_vdev_release
,
3088 .tvnorms
= V4L2_STD_NTSC
| V4L2_STD_PAL
| V4L2_STD_SECAM
,