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.
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/delay.h>
46 #include <linux/slab.h>
47 #include <linux/pci.h>
48 #include <linux/vmalloc.h>
49 #include <linux/wait.h>
51 #include <linux/interrupt.h>
52 #include <linux/i2c.h>
53 #include <linux/i2c-algo-bit.h>
55 #include <linux/spinlock.h>
57 #include <linux/videodev2.h>
58 #include <media/v4l2-common.h>
59 #include <media/v4l2-ioctl.h>
60 #include <media/v4l2-event.h>
61 #include "videocodec.h"
63 #include <asm/byteorder.h>
65 #include <linux/uaccess.h>
66 #include <linux/proc_fs.h>
68 #include <linux/mutex.h>
70 #include "zoran_device.h"
71 #include "zoran_card.h"
74 const struct zoran_format zoran_formats[] = {
76 .name = "15-bit RGB LE",
77 .fourcc = V4L2_PIX_FMT_RGB555,
78 .colorspace = V4L2_COLORSPACE_SRGB,
80 .flags = ZORAN_FORMAT_CAPTURE |
82 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
83 ZR36057_VFESPFR_LittleEndian,
85 .name = "15-bit RGB BE",
86 .fourcc = V4L2_PIX_FMT_RGB555X,
87 .colorspace = V4L2_COLORSPACE_SRGB,
89 .flags = ZORAN_FORMAT_CAPTURE |
91 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
93 .name = "16-bit RGB LE",
94 .fourcc = V4L2_PIX_FMT_RGB565,
95 .colorspace = V4L2_COLORSPACE_SRGB,
97 .flags = ZORAN_FORMAT_CAPTURE |
99 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
100 ZR36057_VFESPFR_LittleEndian,
102 .name = "16-bit RGB BE",
103 .fourcc = V4L2_PIX_FMT_RGB565X,
104 .colorspace = V4L2_COLORSPACE_SRGB,
106 .flags = ZORAN_FORMAT_CAPTURE |
107 ZORAN_FORMAT_OVERLAY,
108 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
110 .name = "24-bit RGB",
111 .fourcc = V4L2_PIX_FMT_BGR24,
112 .colorspace = V4L2_COLORSPACE_SRGB,
114 .flags = ZORAN_FORMAT_CAPTURE |
115 ZORAN_FORMAT_OVERLAY,
116 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
118 .name = "32-bit RGB LE",
119 .fourcc = V4L2_PIX_FMT_BGR32,
120 .colorspace = V4L2_COLORSPACE_SRGB,
122 .flags = ZORAN_FORMAT_CAPTURE |
123 ZORAN_FORMAT_OVERLAY,
124 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
126 .name = "32-bit RGB BE",
127 .fourcc = V4L2_PIX_FMT_RGB32,
128 .colorspace = V4L2_COLORSPACE_SRGB,
130 .flags = ZORAN_FORMAT_CAPTURE |
131 ZORAN_FORMAT_OVERLAY,
132 .vfespfr = ZR36057_VFESPFR_RGB888,
134 .name = "4:2:2, packed, YUYV",
135 .fourcc = V4L2_PIX_FMT_YUYV,
136 .colorspace = V4L2_COLORSPACE_SMPTE170M,
138 .flags = ZORAN_FORMAT_CAPTURE |
139 ZORAN_FORMAT_OVERLAY,
140 .vfespfr = ZR36057_VFESPFR_YUV422,
142 .name = "4:2:2, packed, UYVY",
143 .fourcc = V4L2_PIX_FMT_UYVY,
144 .colorspace = V4L2_COLORSPACE_SMPTE170M,
146 .flags = ZORAN_FORMAT_CAPTURE |
147 ZORAN_FORMAT_OVERLAY,
148 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
150 .name = "Hardware-encoded Motion-JPEG",
151 .fourcc = V4L2_PIX_FMT_MJPEG,
152 .colorspace = V4L2_COLORSPACE_SMPTE170M,
154 .flags = ZORAN_FORMAT_CAPTURE |
155 ZORAN_FORMAT_PLAYBACK |
156 ZORAN_FORMAT_COMPRESSED,
159 #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
161 /* small helper function for calculating buffersizes for v4l2
162 * we calculate the nearest higher power-of-two, which
163 * will be the recommended buffersize */
165 zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
167 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
168 __u32 num = (1024 * 512) / (div);
177 if (result > jpg_bufsize)
184 /* forward references */
185 static void v4l_fbuffer_free(struct zoran_fh *fh);
186 static void jpg_fbuffer_free(struct zoran_fh *fh);
188 /* Set mapping mode */
189 static void map_mode_raw(struct zoran_fh *fh)
191 fh->map_mode = ZORAN_MAP_MODE_RAW;
192 fh->buffers.buffer_size = v4l_bufsize;
193 fh->buffers.num_buffers = v4l_nbufs;
195 static void map_mode_jpg(struct zoran_fh *fh, int play)
197 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
198 fh->buffers.buffer_size = jpg_bufsize;
199 fh->buffers.num_buffers = jpg_nbufs;
201 static inline const char *mode_name(enum zoran_map_mode mode)
203 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
207 * Allocate the V4L grab buffers
209 * These have to be pysically contiguous.
212 static int v4l_fbuffer_alloc(struct zoran_fh *fh)
214 struct zoran *zr = fh->zr;
218 for (i = 0; i < fh->buffers.num_buffers; i++) {
219 if (fh->buffers.buffer[i].v4l.fbuffer)
222 "%s: %s - buffer %d already allocated!?\n",
223 ZR_DEVNAME(zr), __func__, i);
226 mem = kmalloc(fh->buffers.buffer_size,
227 GFP_KERNEL | __GFP_NOWARN);
231 "%s: %s - kmalloc for V4L buf %d failed\n",
232 ZR_DEVNAME(zr), __func__, i);
233 v4l_fbuffer_free(fh);
236 fh->buffers.buffer[i].v4l.fbuffer = mem;
237 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
238 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
239 for (off = 0; off < fh->buffers.buffer_size;
241 SetPageReserved(virt_to_page(mem + off));
244 "%s: %s - V4L frame %d mem %p (bus: 0x%llx)\n",
245 ZR_DEVNAME(zr), __func__, i, mem,
246 (unsigned long long)virt_to_bus(mem));
249 fh->buffers.allocated = 1;
254 /* free the V4L grab buffers */
255 static void v4l_fbuffer_free(struct zoran_fh *fh)
257 struct zoran *zr = fh->zr;
261 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
263 for (i = 0; i < fh->buffers.num_buffers; i++) {
264 if (!fh->buffers.buffer[i].v4l.fbuffer)
267 mem = fh->buffers.buffer[i].v4l.fbuffer;
268 for (off = 0; off < fh->buffers.buffer_size;
270 ClearPageReserved(virt_to_page(mem + off));
271 kfree(fh->buffers.buffer[i].v4l.fbuffer);
272 fh->buffers.buffer[i].v4l.fbuffer = NULL;
275 fh->buffers.allocated = 0;
279 * Allocate the MJPEG grab buffers.
281 * If a Natoma chipset is present and this is a revision 1 zr36057,
282 * each MJPEG buffer needs to be physically contiguous.
283 * (RJ: This statement is from Dave Perks' original driver,
284 * I could never check it because I have a zr36067)
286 * RJ: The contents grab buffers needs never be accessed in the driver.
287 * Therefore there is no need to allocate them with vmalloc in order
288 * to get a contiguous virtual memory space.
289 * I don't understand why many other drivers first allocate them with
290 * vmalloc (which uses internally also get_zeroed_page, but delivers you
291 * virtual addresses) and then again have to make a lot of efforts
292 * to get the physical address.
295 * On big-endian architectures (such as ppc) some extra steps
296 * are needed. When reading and writing to the stat_com array
297 * and fragment buffers, the device expects to see little-
298 * endian values. The use of cpu_to_le32() and le32_to_cpu()
299 * in this function (and one or two others in zoran_device.c)
300 * ensure that these values are always stored in little-endian
301 * form, regardless of architecture. The zr36057 does Very Bad
302 * Things on big endian architectures if the stat_com array
303 * and fragment buffers are not little-endian.
306 static int jpg_fbuffer_alloc(struct zoran_fh *fh)
308 struct zoran *zr = fh->zr;
312 for (i = 0; i < fh->buffers.num_buffers; i++) {
313 if (fh->buffers.buffer[i].jpg.frag_tab)
316 "%s: %s - buffer %d already allocated!?\n",
317 ZR_DEVNAME(zr), __func__, i);
319 /* Allocate fragment table for this buffer */
321 mem = (void *)get_zeroed_page(GFP_KERNEL);
325 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
326 ZR_DEVNAME(zr), __func__, i);
327 jpg_fbuffer_free(fh);
330 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
331 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
333 if (fh->buffers.need_contiguous) {
334 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
338 "%s: %s - kmalloc failed for buffer %d\n",
339 ZR_DEVNAME(zr), __func__, i);
340 jpg_fbuffer_free(fh);
343 fh->buffers.buffer[i].jpg.frag_tab[0] =
344 cpu_to_le32(virt_to_bus(mem));
345 fh->buffers.buffer[i].jpg.frag_tab[1] =
346 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
347 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
348 SetPageReserved(virt_to_page(mem + off));
350 /* jpg_bufsize is already page aligned */
351 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
352 mem = (void *)get_zeroed_page(GFP_KERNEL);
356 "%s: %s - get_zeroed_page failed for buffer %d\n",
357 ZR_DEVNAME(zr), __func__, i);
358 jpg_fbuffer_free(fh);
362 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
363 cpu_to_le32(virt_to_bus(mem));
364 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
365 cpu_to_le32((PAGE_SIZE >> 2) << 1);
366 SetPageReserved(virt_to_page(mem));
369 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
374 KERN_DEBUG "%s: %s - %d KB allocated\n",
375 ZR_DEVNAME(zr), __func__,
376 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
378 fh->buffers.allocated = 1;
383 /* free the MJPEG grab buffers */
384 static void jpg_fbuffer_free(struct zoran_fh *fh)
386 struct zoran *zr = fh->zr;
390 struct zoran_buffer *buffer;
392 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
394 for (i = 0, buffer = &fh->buffers.buffer[0];
395 i < fh->buffers.num_buffers; i++, buffer++) {
396 if (!buffer->jpg.frag_tab)
399 if (fh->buffers.need_contiguous) {
400 frag_tab = buffer->jpg.frag_tab[0];
403 mem = bus_to_virt(le32_to_cpu(frag_tab));
404 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
405 ClearPageReserved(virt_to_page(mem + off));
407 buffer->jpg.frag_tab[0] = 0;
408 buffer->jpg.frag_tab[1] = 0;
411 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
412 frag_tab = buffer->jpg.frag_tab[2 * j];
416 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
417 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
418 buffer->jpg.frag_tab[2 * j] = 0;
419 buffer->jpg.frag_tab[2 * j + 1] = 0;
423 free_page((unsigned long)buffer->jpg.frag_tab);
424 buffer->jpg.frag_tab = NULL;
427 fh->buffers.allocated = 0;
431 * V4L Buffer grabbing
435 zoran_v4l_set_format (struct zoran_fh *fh,
438 const struct zoran_format *format)
440 struct zoran *zr = fh->zr;
443 /* Check size and format of the grab wanted */
445 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
446 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
449 "%s: %s - wrong frame size (%dx%d)\n",
450 ZR_DEVNAME(zr), __func__, width, height);
454 bpp = (format->depth + 7) / 8;
456 /* Check against available buffer size */
457 if (height * width * bpp > fh->buffers.buffer_size) {
460 "%s: %s - video buffer size (%d kB) is too small\n",
461 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
465 /* The video front end needs 4-byte alinged line sizes */
467 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
470 "%s: %s - wrong frame alignment\n",
471 ZR_DEVNAME(zr), __func__);
475 fh->v4l_settings.width = width;
476 fh->v4l_settings.height = height;
477 fh->v4l_settings.format = format;
478 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
483 static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
485 struct zoran *zr = fh->zr;
489 if (!fh->buffers.allocated) {
492 "%s: %s - buffers not yet allocated\n",
493 ZR_DEVNAME(zr), __func__);
497 /* No grabbing outside the buffer range! */
498 if (num >= fh->buffers.num_buffers || num < 0) {
501 "%s: %s - buffer %d is out of range\n",
502 ZR_DEVNAME(zr), __func__, num);
506 spin_lock_irqsave(&zr->spinlock, flags);
508 if (fh->buffers.active == ZORAN_FREE) {
509 if (zr->v4l_buffers.active == ZORAN_FREE) {
510 zr->v4l_buffers = fh->buffers;
511 fh->buffers.active = ZORAN_ACTIVE;
515 "%s: %s - another session is already capturing\n",
516 ZR_DEVNAME(zr), __func__);
521 /* make sure a grab isn't going on currently with this buffer */
523 switch (zr->v4l_buffers.buffer[num].state) {
526 if (zr->v4l_buffers.active == ZORAN_FREE) {
527 fh->buffers.active = ZORAN_FREE;
528 zr->v4l_buffers.allocated = 0;
530 res = -EBUSY; /* what are you doing? */
535 "%s: %s - queueing buffer %d in state DONE!?\n",
536 ZR_DEVNAME(zr), __func__, num);
539 /* since there is at least one unused buffer there's room for at least
540 * one more pend[] entry */
541 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
542 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
543 zr->v4l_buffers.buffer[num].bs.length =
544 fh->v4l_settings.bytesperline *
545 zr->v4l_settings.height;
546 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
551 spin_unlock_irqrestore(&zr->spinlock, flags);
553 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
554 zr->v4l_buffers.active = fh->buffers.active;
560 * Sync on a V4L buffer
563 static int v4l_sync(struct zoran_fh *fh, int frame)
565 struct zoran *zr = fh->zr;
568 if (fh->buffers.active == ZORAN_FREE) {
571 "%s: %s - no grab active for this session\n",
572 ZR_DEVNAME(zr), __func__);
576 /* check passed-in frame number */
577 if (frame >= fh->buffers.num_buffers || frame < 0) {
579 KERN_ERR "%s: %s - frame %d is invalid\n",
580 ZR_DEVNAME(zr), __func__, frame);
584 /* Check if is buffer was queued at all */
585 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
588 "%s: %s - attempt to sync on a buffer which was not queued?\n",
589 ZR_DEVNAME(zr), __func__);
593 mutex_unlock(&zr->lock);
594 /* wait on this buffer to get ready */
595 if (!wait_event_interruptible_timeout(zr->v4l_capq,
596 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ)) {
597 mutex_lock(&zr->lock);
600 mutex_lock(&zr->lock);
601 if (signal_pending(current))
604 /* buffer should now be in BUZ_STATE_DONE */
605 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
607 KERN_ERR "%s: %s - internal state error\n",
608 ZR_DEVNAME(zr), __func__);
610 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
611 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
613 spin_lock_irqsave(&zr->spinlock, flags);
615 /* Check if streaming capture has finished */
616 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
617 zr36057_set_memgrab(zr, 0);
618 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
619 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
620 zr->v4l_buffers.allocated = 0;
624 spin_unlock_irqrestore(&zr->spinlock, flags);
630 * Queue a MJPEG buffer for capture/playback
633 static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
634 enum zoran_codec_mode mode)
636 struct zoran *zr = fh->zr;
640 /* Check if buffers are allocated */
641 if (!fh->buffers.allocated) {
644 "%s: %s - buffers not yet allocated\n",
645 ZR_DEVNAME(zr), __func__);
649 /* No grabbing outside the buffer range! */
650 if (num >= fh->buffers.num_buffers || num < 0) {
653 "%s: %s - buffer %d out of range\n",
654 ZR_DEVNAME(zr), __func__, num);
658 /* what is the codec mode right now? */
659 if (zr->codec_mode == BUZ_MODE_IDLE) {
660 zr->jpg_settings = fh->jpg_settings;
661 } else if (zr->codec_mode != mode) {
662 /* wrong codec mode active - invalid */
665 "%s: %s - codec in wrong mode\n",
666 ZR_DEVNAME(zr), __func__);
670 if (fh->buffers.active == ZORAN_FREE) {
671 if (zr->jpg_buffers.active == ZORAN_FREE) {
672 zr->jpg_buffers = fh->buffers;
673 fh->buffers.active = ZORAN_ACTIVE;
677 "%s: %s - another session is already capturing\n",
678 ZR_DEVNAME(zr), __func__);
683 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
684 /* Ok load up the jpeg codec */
685 zr36057_enable_jpg(zr, mode);
688 spin_lock_irqsave(&zr->spinlock, flags);
691 switch (zr->jpg_buffers.buffer[num].state) {
695 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
696 ZR_DEVNAME(zr), __func__);
699 /* since there is at least one unused buffer there's room for at
700 *least one more pend[] entry */
701 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
702 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
703 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
704 zoran_feed_stat_com(zr);
709 if (zr->jpg_buffers.active == ZORAN_FREE) {
710 fh->buffers.active = ZORAN_FREE;
711 zr->jpg_buffers.allocated = 0;
713 res = -EBUSY; /* what are you doing? */
718 spin_unlock_irqrestore(&zr->spinlock, flags);
720 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
721 zr->jpg_buffers.active = fh->buffers.active;
726 static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
728 struct zoran *zr = fh->zr;
731 /* Does the user want to stop streaming? */
733 if (zr->codec_mode == mode) {
734 if (fh->buffers.active == ZORAN_FREE) {
737 "%s: %s(-1) - session not active\n",
738 ZR_DEVNAME(zr), __func__);
741 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
742 zr->jpg_buffers.allocated = 0;
743 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
748 "%s: %s - stop streaming but not in streaming mode\n",
749 ZR_DEVNAME(zr), __func__);
754 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
757 /* Start the jpeg codec when the first frame is queued */
758 if (!res && zr->jpg_que_head == 1)
765 * Sync on a MJPEG buffer
768 static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
770 struct zoran *zr = fh->zr;
774 if (fh->buffers.active == ZORAN_FREE) {
777 "%s: %s - capture is not currently active\n",
778 ZR_DEVNAME(zr), __func__);
781 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
782 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
785 "%s: %s - codec not in streaming mode\n",
786 ZR_DEVNAME(zr), __func__);
789 mutex_unlock(&zr->lock);
790 if (!wait_event_interruptible_timeout(zr->jpg_capq,
791 (zr->jpg_que_tail != zr->jpg_dma_tail ||
792 zr->jpg_dma_tail == zr->jpg_dma_head),
796 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
798 zr->codec->control(zr->codec, CODEC_G_STATUS,
800 mutex_lock(&zr->lock);
803 "%s: %s - timeout: codec isr=0x%02x\n",
804 ZR_DEVNAME(zr), __func__, isr);
809 mutex_lock(&zr->lock);
810 if (signal_pending(current))
813 spin_lock_irqsave(&zr->spinlock, flags);
815 if (zr->jpg_dma_tail != zr->jpg_dma_head)
816 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
818 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
820 /* buffer should now be in BUZ_STATE_DONE */
821 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
823 KERN_ERR "%s: %s - internal state error\n",
824 ZR_DEVNAME(zr), __func__);
826 *bs = zr->jpg_buffers.buffer[frame].bs;
828 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
829 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
831 spin_unlock_irqrestore(&zr->spinlock, flags);
836 static void zoran_open_init_session(struct zoran_fh *fh)
839 struct zoran *zr = fh->zr;
841 /* Per default, map the V4L Buffers */
844 /* take over the card's current settings */
845 fh->overlay_settings = zr->overlay_settings;
846 fh->overlay_settings.is_set = 0;
847 fh->overlay_settings.format = zr->overlay_settings.format;
848 fh->overlay_active = ZORAN_FREE;
851 fh->v4l_settings = zr->v4l_settings;
853 fh->jpg_settings = zr->jpg_settings;
856 memset(&fh->buffers, 0, sizeof(fh->buffers));
857 for (i = 0; i < MAX_FRAME; i++) {
858 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
859 fh->buffers.buffer[i].bs.frame = i;
861 fh->buffers.allocated = 0;
862 fh->buffers.active = ZORAN_FREE;
865 static void zoran_close_end_session(struct zoran_fh *fh)
867 struct zoran *zr = fh->zr;
870 if (fh->overlay_active != ZORAN_FREE) {
871 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
872 zr->v4l_overlay_active = 0;
873 if (!zr->v4l_memgrab_active)
874 zr36057_overlay(zr, 0);
875 zr->overlay_mask = NULL;
878 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
880 if (fh->buffers.active != ZORAN_FREE) {
883 spin_lock_irqsave(&zr->spinlock, flags);
884 zr36057_set_memgrab(zr, 0);
885 zr->v4l_buffers.allocated = 0;
886 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
887 spin_unlock_irqrestore(&zr->spinlock, flags);
891 if (fh->buffers.allocated)
892 v4l_fbuffer_free(fh);
895 if (fh->buffers.active != ZORAN_FREE) {
896 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
897 zr->jpg_buffers.allocated = 0;
898 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
902 if (fh->buffers.allocated)
903 jpg_fbuffer_free(fh);
908 * Open a zoran card. Right now the flags stuff is just playing
911 static int zoran_open(struct file *file)
913 struct zoran *zr = video_drvdata(file);
915 int res, first_open = 0;
917 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
918 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
920 mutex_lock(&zr->lock);
922 if (zr->user >= 2048) {
923 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
924 ZR_DEVNAME(zr), zr->user);
929 /* now, create the open()-specific file_ops struct */
930 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
934 "%s: %s - allocation of zoran_fh failed\n",
935 ZR_DEVNAME(zr), __func__);
939 v4l2_fh_init(&fh->fh, video_devdata(file));
941 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
944 kmalloc(array3_size((768 + 31) / 32, 576, 4), GFP_KERNEL);
945 if (!fh->overlay_mask) {
948 "%s: %s - allocation of overlay_mask failed\n",
949 ZR_DEVNAME(zr), __func__);
957 /* default setup - TODO: look at flags */
958 if (first_open) { /* First device open */
960 zoran_open_init_params(zr);
961 zoran_init_hardware(zr);
963 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
966 /* set file_ops stuff */
967 file->private_data = fh;
969 zoran_open_init_session(fh);
970 v4l2_fh_add(&fh->fh);
971 mutex_unlock(&zr->lock);
976 v4l2_fh_exit(&fh->fh);
979 mutex_unlock(&zr->lock);
981 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
982 ZR_DEVNAME(zr), res, zr->user);
988 zoran_close(struct file *file)
990 struct zoran_fh *fh = file->private_data;
991 struct zoran *zr = fh->zr;
993 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
994 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
996 /* kernel locks (fs/device.c), so don't do that ourselves
997 * (prevents deadlocks) */
998 mutex_lock(&zr->lock);
1000 zoran_close_end_session(fh);
1002 if (zr->user-- == 1) { /* Last process */
1003 /* Clean up JPEG process */
1004 wake_up_interruptible(&zr->jpg_capq);
1005 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1006 zr->jpg_buffers.allocated = 0;
1007 zr->jpg_buffers.active = ZORAN_FREE;
1009 /* disable interrupts */
1010 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1012 if (zr36067_debug > 1)
1013 print_interrupts(zr);
1016 zr->v4l_overlay_active = 0;
1017 zr36057_overlay(zr, 0);
1018 zr->overlay_mask = NULL;
1021 wake_up_interruptible(&zr->v4l_capq);
1022 zr36057_set_memgrab(zr, 0);
1023 zr->v4l_buffers.allocated = 0;
1024 zr->v4l_buffers.active = ZORAN_FREE;
1025 zoran_set_pci_master(zr, 0);
1027 if (!pass_through) { /* Switch to color bar */
1028 decoder_call(zr, video, s_stream, 0);
1029 encoder_call(zr, video, s_routing, 2, 0, 0);
1032 mutex_unlock(&zr->lock);
1034 v4l2_fh_del(&fh->fh);
1035 v4l2_fh_exit(&fh->fh);
1036 kfree(fh->overlay_mask);
1039 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
1044 static int setup_fbuffer(struct zoran_fh *fh,
1046 const struct zoran_format *fmt,
1051 struct zoran *zr = fh->zr;
1053 /* (Ronald) v4l/v4l2 guidelines */
1054 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1057 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1058 ALi Magik (that needs very low latency while the card needs a
1059 higher value always) */
1061 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1064 /* we need a bytesperline value, even if not given */
1066 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1069 if (zr->overlay_active) {
1070 /* dzjee... stupid users... don't even bother to turn off
1071 * overlay before changing the memory location...
1072 * normally, we would return errors here. However, one of
1073 * the tools that does this is... xawtv! and since xawtv
1074 * is used by +/- 99% of the users, we'd rather be user-
1075 * friendly and silently do as if nothing went wrong */
1078 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1079 ZR_DEVNAME(zr), __func__);
1080 zr36057_overlay(zr, 0);
1084 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1087 "%s: %s - no valid overlay format given\n",
1088 ZR_DEVNAME(zr), __func__);
1091 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1094 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1095 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
1098 if (bytesperline & 3) {
1101 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1102 ZR_DEVNAME(zr), __func__, bytesperline);
1106 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1107 zr->vbuf_height = height;
1108 zr->vbuf_width = width;
1109 zr->vbuf_depth = fmt->depth;
1110 zr->overlay_settings.format = fmt;
1111 zr->vbuf_bytesperline = bytesperline;
1113 /* The user should set new window parameters */
1114 zr->overlay_settings.is_set = 0;
1120 static int setup_window(struct zoran_fh *fh,
1125 struct v4l2_clip __user *clips,
1126 unsigned int clipcount,
1127 void __user *bitmap)
1129 struct zoran *zr = fh->zr;
1130 struct v4l2_clip *vcp = NULL;
1134 if (!zr->vbuf_base) {
1137 "%s: %s - frame buffer has to be set first\n",
1138 ZR_DEVNAME(zr), __func__);
1142 if (!fh->overlay_settings.format) {
1145 "%s: %s - no overlay format set\n",
1146 ZR_DEVNAME(zr), __func__);
1150 if (clipcount > 2048) {
1153 "%s: %s - invalid clipcount\n",
1154 ZR_DEVNAME(zr), __func__);
1159 * The video front end needs 4-byte alinged line sizes, we correct that
1160 * silently here if necessary
1162 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
1163 end = (x + width) & ~1; /* round down */
1164 x = (x + 1) & ~1; /* round up */
1168 if (zr->vbuf_depth == 24) {
1169 end = (x + width) & ~3; /* round down */
1170 x = (x + 3) & ~3; /* round up */
1174 if (width > BUZ_MAX_WIDTH)
1175 width = BUZ_MAX_WIDTH;
1176 if (height > BUZ_MAX_HEIGHT)
1177 height = BUZ_MAX_HEIGHT;
1179 /* Check for invalid parameters */
1180 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1181 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1184 "%s: %s - width = %d or height = %d invalid\n",
1185 ZR_DEVNAME(zr), __func__, width, height);
1189 fh->overlay_settings.x = x;
1190 fh->overlay_settings.y = y;
1191 fh->overlay_settings.width = width;
1192 fh->overlay_settings.height = height;
1193 fh->overlay_settings.clipcount = clipcount;
1196 * If an overlay is running, we have to switch it off
1197 * and switch it on again in order to get the new settings in effect.
1199 * We also want to avoid that the overlay mask is written
1200 * when an overlay is running.
1203 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1204 zr->overlay_active != ZORAN_FREE &&
1205 fh->overlay_active != ZORAN_FREE;
1207 zr36057_overlay(zr, 0);
1210 * Write the overlay mask if clips are wanted.
1211 * We prefer a bitmap.
1214 /* fake value - it just means we want clips */
1215 fh->overlay_settings.clipcount = 1;
1217 if (copy_from_user(fh->overlay_mask, bitmap,
1218 (width * height + 7) / 8)) {
1221 } else if (clipcount) {
1222 /* write our own bitmap from the clips */
1223 vcp = vmalloc(array_size(sizeof(struct v4l2_clip),
1228 "%s: %s - Alloc of clip mask failed\n",
1229 ZR_DEVNAME(zr), __func__);
1233 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
1237 write_overlay_mask(fh, vcp, clipcount);
1241 fh->overlay_settings.is_set = 1;
1242 if (fh->overlay_active != ZORAN_FREE &&
1243 zr->overlay_active != ZORAN_FREE)
1244 zr->overlay_settings = fh->overlay_settings;
1247 zr36057_overlay(zr, 1);
1249 /* Make sure the changes come into effect */
1250 return wait_grab_pending(zr);
1253 static int setup_overlay(struct zoran_fh *fh, int on)
1255 struct zoran *zr = fh->zr;
1257 /* If there is nothing to do, return immediately */
1258 if ((on && fh->overlay_active != ZORAN_FREE) ||
1259 (!on && fh->overlay_active == ZORAN_FREE))
1262 /* check whether we're touching someone else's overlay */
1263 if (on && zr->overlay_active != ZORAN_FREE &&
1264 fh->overlay_active == ZORAN_FREE) {
1267 "%s: %s - overlay is already active for another session\n",
1268 ZR_DEVNAME(zr), __func__);
1271 if (!on && zr->overlay_active != ZORAN_FREE &&
1272 fh->overlay_active == ZORAN_FREE) {
1275 "%s: %s - you cannot cancel someone else's session\n",
1276 ZR_DEVNAME(zr), __func__);
1281 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1282 zr->v4l_overlay_active = 0;
1283 /* When a grab is running, the video simply
1284 * won't be switched on any more */
1285 if (!zr->v4l_memgrab_active)
1286 zr36057_overlay(zr, 0);
1287 zr->overlay_mask = NULL;
1289 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
1292 "%s: %s - buffer or window not set\n",
1293 ZR_DEVNAME(zr), __func__);
1296 if (!fh->overlay_settings.format) {
1299 "%s: %s - no overlay format set\n",
1300 ZR_DEVNAME(zr), __func__);
1303 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1304 zr->v4l_overlay_active = 1;
1305 zr->overlay_mask = fh->overlay_mask;
1306 zr->overlay_settings = fh->overlay_settings;
1307 if (!zr->v4l_memgrab_active)
1308 zr36057_overlay(zr, 1);
1309 /* When a grab is running, the video will be
1310 * switched on when grab is finished */
1313 /* Make sure the changes come into effect */
1314 return wait_grab_pending(zr);
1317 /* get the status of a buffer in the clients buffer queue */
1318 static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1319 struct v4l2_buffer *buf, int num)
1321 struct zoran *zr = fh->zr;
1322 unsigned long flags;
1324 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1326 switch (fh->map_mode) {
1327 case ZORAN_MAP_MODE_RAW:
1329 if (num < 0 || num >= fh->buffers.num_buffers ||
1330 !fh->buffers.allocated) {
1333 "%s: %s - wrong number or buffers not allocated\n",
1334 ZR_DEVNAME(zr), __func__);
1338 spin_lock_irqsave(&zr->spinlock, flags);
1341 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1342 ZR_DEVNAME(zr), __func__,
1343 "FAL"[fh->buffers.active], num,
1344 "UPMD"[zr->v4l_buffers.buffer[num].state],
1345 fh->buffers.buffer[num].map ? 'Y' : 'N');
1346 spin_unlock_irqrestore(&zr->spinlock, flags);
1348 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1349 buf->length = fh->buffers.buffer_size;
1352 buf->bytesused = fh->buffers.buffer[num].bs.length;
1353 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1354 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1355 buf->sequence = fh->buffers.buffer[num].bs.seq;
1356 buf->flags |= V4L2_BUF_FLAG_DONE;
1357 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1359 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1362 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1363 buf->field = V4L2_FIELD_TOP;
1365 buf->field = V4L2_FIELD_INTERLACED;
1369 case ZORAN_MAP_MODE_JPG_REC:
1370 case ZORAN_MAP_MODE_JPG_PLAY:
1373 if (num < 0 || num >= fh->buffers.num_buffers ||
1374 !fh->buffers.allocated) {
1377 "%s: %s - wrong number or buffers not allocated\n",
1378 ZR_DEVNAME(zr), __func__);
1382 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1383 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1384 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1385 buf->length = fh->buffers.buffer_size;
1387 /* these variables are only written after frame has been captured */
1388 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1389 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1390 buf->sequence = fh->buffers.buffer[num].bs.seq;
1391 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1392 buf->bytesused = fh->buffers.buffer[num].bs.length;
1393 buf->flags |= V4L2_BUF_FLAG_DONE;
1395 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1398 /* which fields are these? */
1399 if (fh->jpg_settings.TmpDcm != 1)
1400 buf->field = fh->jpg_settings.odd_even ?
1401 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
1403 buf->field = fh->jpg_settings.odd_even ?
1404 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
1412 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1413 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
1417 buf->memory = V4L2_MEMORY_MMAP;
1419 buf->m.offset = buf->length * num;
1425 zoran_set_norm (struct zoran *zr,
1430 if (zr->v4l_buffers.active != ZORAN_FREE ||
1431 zr->jpg_buffers.active != ZORAN_FREE) {
1434 "%s: %s called while in playback/capture mode\n",
1435 ZR_DEVNAME(zr), __func__);
1439 if (!(norm & zr->card.norms)) {
1441 KERN_ERR "%s: %s - unsupported norm %llx\n",
1442 ZR_DEVNAME(zr), __func__, norm);
1446 if (norm & V4L2_STD_SECAM)
1447 zr->timing = zr->card.tvn[2];
1448 else if (norm & V4L2_STD_NTSC)
1449 zr->timing = zr->card.tvn[1];
1451 zr->timing = zr->card.tvn[0];
1453 /* We switch overlay off and on since a change in the
1454 * norm needs different VFE settings */
1455 on = zr->overlay_active && !zr->v4l_memgrab_active;
1457 zr36057_overlay(zr, 0);
1459 decoder_call(zr, video, s_std, norm);
1460 encoder_call(zr, video, s_std_output, norm);
1463 zr36057_overlay(zr, 1);
1465 /* Make sure the changes come into effect */
1472 zoran_set_input (struct zoran *zr,
1475 if (input == zr->input) {
1479 if (zr->v4l_buffers.active != ZORAN_FREE ||
1480 zr->jpg_buffers.active != ZORAN_FREE) {
1483 "%s: %s called while in playback/capture mode\n",
1484 ZR_DEVNAME(zr), __func__);
1488 if (input < 0 || input >= zr->card.inputs) {
1491 "%s: %s - unsupported input %d\n",
1492 ZR_DEVNAME(zr), __func__, input);
1498 decoder_call(zr, video, s_routing,
1499 zr->card.input[input].muxsel, 0, 0);
1508 static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1510 struct zoran_fh *fh = __fh;
1511 struct zoran *zr = fh->zr;
1513 strlcpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
1514 strlcpy(cap->driver, "zoran", sizeof(cap->driver));
1515 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1516 pci_name(zr->pci_dev));
1517 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1518 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
1519 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1523 static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
1525 unsigned int num, i;
1527 for (num = i = 0; i < NUM_FORMATS; i++) {
1528 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1529 strncpy(fmt->description, zoran_formats[i].name,
1530 sizeof(fmt->description) - 1);
1531 /* fmt struct pre-zeroed, so adding '\0' not needed */
1532 fmt->pixelformat = zoran_formats[i].fourcc;
1533 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1534 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1541 static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1542 struct v4l2_fmtdesc *f)
1544 struct zoran_fh *fh = __fh;
1545 struct zoran *zr = fh->zr;
1547 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1550 static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1551 struct v4l2_fmtdesc *f)
1553 struct zoran_fh *fh = __fh;
1554 struct zoran *zr = fh->zr;
1556 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1559 static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1560 struct v4l2_fmtdesc *f)
1562 struct zoran_fh *fh = __fh;
1563 struct zoran *zr = fh->zr;
1565 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1568 static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1569 struct v4l2_format *fmt)
1571 struct zoran_fh *fh = __fh;
1573 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
1574 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
1575 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1576 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1577 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1578 if (fh->jpg_settings.TmpDcm == 1)
1579 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1580 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1582 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1583 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1584 fmt->fmt.pix.bytesperline = 0;
1585 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1590 static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1591 struct v4l2_format *fmt)
1593 struct zoran_fh *fh = __fh;
1594 struct zoran *zr = fh->zr;
1596 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1597 return zoran_g_fmt_vid_out(file, fh, fmt);
1599 fmt->fmt.pix.width = fh->v4l_settings.width;
1600 fmt->fmt.pix.height = fh->v4l_settings.height;
1601 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1602 fh->v4l_settings.height;
1603 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1604 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1605 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1606 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1607 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1609 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1613 static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1614 struct v4l2_format *fmt)
1616 struct zoran_fh *fh = __fh;
1617 struct zoran *zr = fh->zr;
1619 fmt->fmt.win.w.left = fh->overlay_settings.x;
1620 fmt->fmt.win.w.top = fh->overlay_settings.y;
1621 fmt->fmt.win.w.width = fh->overlay_settings.width;
1622 fmt->fmt.win.w.height = fh->overlay_settings.height;
1623 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1624 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1626 fmt->fmt.win.field = V4L2_FIELD_TOP;
1631 static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1632 struct v4l2_format *fmt)
1634 struct zoran_fh *fh = __fh;
1635 struct zoran *zr = fh->zr;
1637 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1638 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1639 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1640 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1641 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1642 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1643 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1644 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1649 static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
1650 struct v4l2_format *fmt)
1652 struct zoran_fh *fh = __fh;
1653 struct zoran *zr = fh->zr;
1654 struct zoran_jpg_settings settings;
1657 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1660 settings = fh->jpg_settings;
1662 /* we actually need to set 'real' parameters now */
1663 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
1664 settings.TmpDcm = 1;
1666 settings.TmpDcm = 2;
1667 settings.decimation = 0;
1668 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
1669 settings.VerDcm = 2;
1671 settings.VerDcm = 1;
1672 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
1673 settings.HorDcm = 4;
1674 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
1675 settings.HorDcm = 2;
1677 settings.HorDcm = 1;
1678 if (settings.TmpDcm == 1)
1679 settings.field_per_buff = 2;
1681 settings.field_per_buff = 1;
1683 if (settings.HorDcm > 1) {
1684 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
1685 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
1688 settings.img_width = BUZ_MAX_WIDTH;
1692 res = zoran_check_jpg_settings(zr, &settings, 1);
1696 /* tell the user what we actually did */
1697 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
1698 fmt->fmt.pix.height = settings.img_height * 2 /
1699 (settings.TmpDcm * settings.VerDcm);
1700 if (settings.TmpDcm == 1)
1701 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1702 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1704 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1705 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1707 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
1708 fmt->fmt.pix.bytesperline = 0;
1709 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1713 static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
1714 struct v4l2_format *fmt)
1716 struct zoran_fh *fh = __fh;
1717 struct zoran *zr = fh->zr;
1721 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
1722 return zoran_try_fmt_vid_out(file, fh, fmt);
1724 for (i = 0; i < NUM_FORMATS; i++)
1725 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
1728 if (i == NUM_FORMATS)
1731 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
1732 v4l_bound_align_image(
1733 &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
1734 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
1738 static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
1739 struct v4l2_format *fmt)
1741 struct zoran_fh *fh = __fh;
1744 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
1745 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
1746 fmt->fmt.win.w.width,
1747 fmt->fmt.win.w.height,
1748 fmt->fmt.win.clipcount,
1749 fmt->fmt.win.bitmap);
1750 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
1751 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
1752 (struct v4l2_clip __user *)fmt->fmt.win.clips,
1753 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
1757 static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
1758 struct v4l2_format *fmt)
1760 struct zoran_fh *fh = __fh;
1761 struct zoran *zr = fh->zr;
1762 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
1763 struct zoran_jpg_settings settings;
1766 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
1767 fmt->fmt.pix.width, fmt->fmt.pix.height,
1768 fmt->fmt.pix.pixelformat,
1769 (char *) &printformat);
1770 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
1773 if (fh->buffers.allocated) {
1774 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
1780 settings = fh->jpg_settings;
1782 /* we actually need to set 'real' parameters now */
1783 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
1784 settings.TmpDcm = 1;
1786 settings.TmpDcm = 2;
1787 settings.decimation = 0;
1788 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
1789 settings.VerDcm = 2;
1791 settings.VerDcm = 1;
1792 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
1793 settings.HorDcm = 4;
1794 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
1795 settings.HorDcm = 2;
1797 settings.HorDcm = 1;
1798 if (settings.TmpDcm == 1)
1799 settings.field_per_buff = 2;
1801 settings.field_per_buff = 1;
1803 if (settings.HorDcm > 1) {
1804 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
1805 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
1808 settings.img_width = BUZ_MAX_WIDTH;
1812 res = zoran_check_jpg_settings(zr, &settings, 0);
1816 /* it's ok, so set them */
1817 fh->jpg_settings = settings;
1819 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
1820 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1822 /* tell the user what we actually did */
1823 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
1824 fmt->fmt.pix.height = settings.img_height * 2 /
1825 (settings.TmpDcm * settings.VerDcm);
1826 if (settings.TmpDcm == 1)
1827 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1828 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
1830 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1831 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1832 fmt->fmt.pix.bytesperline = 0;
1833 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
1834 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1838 static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
1839 struct v4l2_format *fmt)
1841 struct zoran_fh *fh = __fh;
1842 struct zoran *zr = fh->zr;
1846 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
1847 return zoran_s_fmt_vid_out(file, fh, fmt);
1849 for (i = 0; i < NUM_FORMATS; i++)
1850 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
1852 if (i == NUM_FORMATS) {
1853 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
1854 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
1858 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
1859 fh->buffers.active != ZORAN_FREE) {
1860 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
1865 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
1866 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
1867 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
1868 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
1872 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
1877 /* tell the user the results/missing stuff */
1878 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1879 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
1880 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1881 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1882 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1884 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1888 static int zoran_g_fbuf(struct file *file, void *__fh,
1889 struct v4l2_framebuffer *fb)
1891 struct zoran_fh *fh = __fh;
1892 struct zoran *zr = fh->zr;
1894 memset(fb, 0, sizeof(*fb));
1895 fb->base = zr->vbuf_base;
1896 fb->fmt.width = zr->vbuf_width;
1897 fb->fmt.height = zr->vbuf_height;
1898 if (zr->overlay_settings.format)
1899 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
1900 fb->fmt.bytesperline = zr->vbuf_bytesperline;
1901 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1902 fb->fmt.field = V4L2_FIELD_INTERLACED;
1903 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1908 static int zoran_s_fbuf(struct file *file, void *__fh,
1909 const struct v4l2_framebuffer *fb)
1911 struct zoran_fh *fh = __fh;
1912 struct zoran *zr = fh->zr;
1914 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
1916 for (i = 0; i < NUM_FORMATS; i++)
1917 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
1919 if (i == NUM_FORMATS) {
1920 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
1921 ZR_DEVNAME(zr), fb->fmt.pixelformat,
1922 (char *)&printformat);
1926 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
1927 fb->fmt.height, fb->fmt.bytesperline);
1932 static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
1934 struct zoran_fh *fh = __fh;
1937 res = setup_overlay(fh, on);
1942 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
1944 static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
1946 struct zoran_fh *fh = __fh;
1947 struct zoran *zr = fh->zr;
1950 if (req->memory != V4L2_MEMORY_MMAP) {
1953 "%s: only MEMORY_MMAP capture is supported, not %d\n",
1954 ZR_DEVNAME(zr), req->memory);
1958 if (req->count == 0)
1959 return zoran_streamoff(file, fh, req->type);
1961 if (fh->buffers.allocated) {
1964 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
1970 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
1971 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1972 /* control user input */
1975 if (req->count > v4l_nbufs)
1976 req->count = v4l_nbufs;
1978 /* The next mmap will map the V4L buffers */
1980 fh->buffers.num_buffers = req->count;
1982 if (v4l_fbuffer_alloc(fh)) {
1986 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
1987 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
1988 /* we need to calculate size ourselves now */
1991 if (req->count > jpg_nbufs)
1992 req->count = jpg_nbufs;
1994 /* The next mmap will map the MJPEG buffers */
1995 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
1996 fh->buffers.num_buffers = req->count;
1997 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1999 if (jpg_fbuffer_alloc(fh)) {
2006 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2007 ZR_DEVNAME(zr), req->type);
2014 static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2016 struct zoran_fh *fh = __fh;
2019 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
2024 static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2026 struct zoran_fh *fh = __fh;
2027 struct zoran *zr = fh->zr;
2028 int res = 0, codec_mode, buf_type;
2030 switch (fh->map_mode) {
2031 case ZORAN_MAP_MODE_RAW:
2032 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2034 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2035 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2040 res = zoran_v4l_queue_frame(fh, buf->index);
2043 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
2044 zr36057_set_memgrab(zr, 1);
2047 case ZORAN_MAP_MODE_JPG_REC:
2048 case ZORAN_MAP_MODE_JPG_PLAY:
2049 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2050 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2051 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2053 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2054 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2057 if (buf->type != buf_type) {
2059 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2060 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2065 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
2068 if (zr->codec_mode == BUZ_MODE_IDLE &&
2069 fh->buffers.active == ZORAN_LOCKED)
2070 zr36057_enable_jpg(zr, codec_mode);
2076 "%s: VIDIOC_QBUF - unsupported type %d\n",
2077 ZR_DEVNAME(zr), buf->type);
2084 static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2086 struct zoran_fh *fh = __fh;
2087 struct zoran *zr = fh->zr;
2088 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2090 switch (fh->map_mode) {
2091 case ZORAN_MAP_MODE_RAW:
2092 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2094 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2095 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2100 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2101 if (file->f_flags & O_NONBLOCK &&
2102 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2106 res = v4l_sync(fh, num);
2109 zr->v4l_sync_tail++;
2110 res = zoran_v4l2_buffer_status(fh, buf, num);
2113 case ZORAN_MAP_MODE_JPG_REC:
2114 case ZORAN_MAP_MODE_JPG_PLAY:
2116 struct zoran_sync bs;
2118 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2119 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2121 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2123 if (buf->type != buf_type) {
2125 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2126 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2131 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2133 if (file->f_flags & O_NONBLOCK &&
2134 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2138 bs.frame = 0; /* suppress compiler warning */
2139 res = jpg_sync(fh, &bs);
2142 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
2148 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2149 ZR_DEVNAME(zr), buf->type);
2156 static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2158 struct zoran_fh *fh = __fh;
2159 struct zoran *zr = fh->zr;
2162 switch (fh->map_mode) {
2163 case ZORAN_MAP_MODE_RAW: /* raw capture */
2164 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
2165 fh->buffers.active != ZORAN_ACTIVE) {
2170 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2171 zr->v4l_settings = fh->v4l_settings;
2173 zr->v4l_sync_tail = zr->v4l_pend_tail;
2174 if (!zr->v4l_memgrab_active &&
2175 zr->v4l_pend_head != zr->v4l_pend_tail) {
2176 zr36057_set_memgrab(zr, 1);
2180 case ZORAN_MAP_MODE_JPG_REC:
2181 case ZORAN_MAP_MODE_JPG_PLAY:
2182 /* what is the codec mode right now? */
2183 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
2184 fh->buffers.active != ZORAN_ACTIVE) {
2189 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
2191 if (zr->jpg_que_head != zr->jpg_que_tail) {
2192 /* Start the jpeg codec when the first frame is queued */
2200 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2201 ZR_DEVNAME(zr), fh->map_mode);
2208 static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2210 struct zoran_fh *fh = __fh;
2211 struct zoran *zr = fh->zr;
2213 unsigned long flags;
2215 switch (fh->map_mode) {
2216 case ZORAN_MAP_MODE_RAW: /* raw capture */
2217 if (fh->buffers.active == ZORAN_FREE &&
2218 zr->v4l_buffers.active != ZORAN_FREE) {
2219 res = -EPERM; /* stay off other's settings! */
2222 if (zr->v4l_buffers.active == ZORAN_FREE)
2225 spin_lock_irqsave(&zr->spinlock, flags);
2226 /* unload capture */
2227 if (zr->v4l_memgrab_active) {
2229 zr36057_set_memgrab(zr, 0);
2232 for (i = 0; i < fh->buffers.num_buffers; i++)
2233 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
2234 fh->buffers = zr->v4l_buffers;
2236 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2238 zr->v4l_grab_seq = 0;
2239 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2240 zr->v4l_sync_tail = 0;
2242 spin_unlock_irqrestore(&zr->spinlock, flags);
2246 case ZORAN_MAP_MODE_JPG_REC:
2247 case ZORAN_MAP_MODE_JPG_PLAY:
2248 if (fh->buffers.active == ZORAN_FREE &&
2249 zr->jpg_buffers.active != ZORAN_FREE) {
2250 res = -EPERM; /* stay off other's settings! */
2253 if (zr->jpg_buffers.active == ZORAN_FREE)
2256 res = jpg_qbuf(fh, -1,
2257 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2258 BUZ_MODE_MOTION_COMPRESS :
2259 BUZ_MODE_MOTION_DECOMPRESS);
2265 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2266 ZR_DEVNAME(zr), fh->map_mode);
2272 static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2274 struct zoran_fh *fh = __fh;
2275 struct zoran *zr = fh->zr;
2281 static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id std)
2283 struct zoran_fh *fh = __fh;
2284 struct zoran *zr = fh->zr;
2287 res = zoran_set_norm(zr, std);
2291 res = wait_grab_pending(zr);
2295 static int zoran_enum_input(struct file *file, void *__fh,
2296 struct v4l2_input *inp)
2298 struct zoran_fh *fh = __fh;
2299 struct zoran *zr = fh->zr;
2301 if (inp->index >= zr->card.inputs)
2304 strncpy(inp->name, zr->card.input[inp->index].name,
2305 sizeof(inp->name) - 1);
2306 inp->type = V4L2_INPUT_TYPE_CAMERA;
2307 inp->std = V4L2_STD_ALL;
2309 /* Get status of video decoder */
2310 decoder_call(zr, video, g_input_status, &inp->status);
2314 static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2316 struct zoran_fh *fh = __fh;
2317 struct zoran *zr = fh->zr;
2324 static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2326 struct zoran_fh *fh = __fh;
2327 struct zoran *zr = fh->zr;
2330 res = zoran_set_input(zr, input);
2334 /* Make sure the changes come into effect */
2335 res = wait_grab_pending(zr);
2339 static int zoran_enum_output(struct file *file, void *__fh,
2340 struct v4l2_output *outp)
2342 if (outp->index != 0)
2346 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2347 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2352 static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2359 static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2367 /* cropping (sub-frame capture) */
2368 static int zoran_g_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
2370 struct zoran_fh *fh = __fh;
2371 struct zoran *zr = fh->zr;
2373 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2374 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2377 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2379 "%s: VIDIOC_G_SELECTION - subcapture only supported for compressed capture\n",
2384 switch (sel->target) {
2385 case V4L2_SEL_TGT_CROP:
2386 sel->r.top = fh->jpg_settings.img_y;
2387 sel->r.left = fh->jpg_settings.img_x;
2388 sel->r.width = fh->jpg_settings.img_width;
2389 sel->r.height = fh->jpg_settings.img_height;
2391 case V4L2_SEL_TGT_CROP_DEFAULT:
2392 sel->r.top = sel->r.left = 0;
2393 sel->r.width = BUZ_MIN_WIDTH;
2394 sel->r.height = BUZ_MIN_HEIGHT;
2396 case V4L2_SEL_TGT_CROP_BOUNDS:
2397 sel->r.top = sel->r.left = 0;
2398 sel->r.width = BUZ_MAX_WIDTH;
2399 sel->r.height = BUZ_MAX_HEIGHT;
2407 static int zoran_s_selection(struct file *file, void *__fh, struct v4l2_selection *sel)
2409 struct zoran_fh *fh = __fh;
2410 struct zoran *zr = fh->zr;
2411 struct zoran_jpg_settings settings;
2414 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2415 sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2418 if (sel->target != V4L2_SEL_TGT_CROP)
2421 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2423 "%s: VIDIOC_S_SELECTION - subcapture only supported for compressed capture\n",
2428 settings = fh->jpg_settings;
2430 if (fh->buffers.allocated) {
2432 "%s: VIDIOC_S_SELECTION - cannot change settings while active\n",
2437 /* move into a form that we understand */
2438 settings.img_x = sel->r.left;
2439 settings.img_y = sel->r.top;
2440 settings.img_width = sel->r.width;
2441 settings.img_height = sel->r.height;
2443 /* check validity */
2444 res = zoran_check_jpg_settings(zr, &settings, 0);
2449 fh->jpg_settings = settings;
2453 static int zoran_g_jpegcomp(struct file *file, void *__fh,
2454 struct v4l2_jpegcompression *params)
2456 struct zoran_fh *fh = __fh;
2457 memset(params, 0, sizeof(*params));
2459 params->quality = fh->jpg_settings.jpg_comp.quality;
2460 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2461 memcpy(params->APP_data,
2462 fh->jpg_settings.jpg_comp.APP_data,
2463 fh->jpg_settings.jpg_comp.APP_len);
2464 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2465 memcpy(params->COM_data,
2466 fh->jpg_settings.jpg_comp.COM_data,
2467 fh->jpg_settings.jpg_comp.COM_len);
2468 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2469 params->jpeg_markers =
2470 fh->jpg_settings.jpg_comp.jpeg_markers;
2475 static int zoran_s_jpegcomp(struct file *file, void *__fh,
2476 const struct v4l2_jpegcompression *params)
2478 struct zoran_fh *fh = __fh;
2479 struct zoran *zr = fh->zr;
2481 struct zoran_jpg_settings settings;
2483 settings = fh->jpg_settings;
2485 settings.jpg_comp = *params;
2487 if (fh->buffers.active != ZORAN_FREE) {
2488 dprintk(1, KERN_WARNING
2489 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
2495 res = zoran_check_jpg_settings(zr, &settings, 0);
2498 if (!fh->buffers.allocated)
2499 fh->buffers.buffer_size =
2500 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2501 fh->jpg_settings.jpg_comp = settings.jpg_comp;
2506 zoran_poll (struct file *file,
2509 struct zoran_fh *fh = file->private_data;
2510 struct zoran *zr = fh->zr;
2511 __poll_t res = v4l2_ctrl_poll(file, wait);
2513 unsigned long flags;
2515 /* we should check whether buffers are ready to be synced on
2516 * (w/o waits - O_NONBLOCK) here
2517 * if ready for read (sync), return EPOLLIN|EPOLLRDNORM,
2518 * if ready for write (sync), return EPOLLOUT|EPOLLWRNORM,
2519 * if error, return EPOLLERR,
2520 * if no buffers queued or so, return EPOLLNVAL
2523 switch (fh->map_mode) {
2524 case ZORAN_MAP_MODE_RAW:
2525 poll_wait(file, &zr->v4l_capq, wait);
2526 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2528 spin_lock_irqsave(&zr->spinlock, flags);
2531 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
2532 ZR_DEVNAME(zr), __func__,
2533 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
2534 "UPMD"[zr->v4l_buffers.buffer[frame].state],
2535 zr->v4l_pend_tail, zr->v4l_pend_head);
2536 /* Process is the one capturing? */
2537 if (fh->buffers.active != ZORAN_FREE &&
2538 /* Buffer ready to DQBUF? */
2539 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
2540 res |= EPOLLIN | EPOLLRDNORM;
2541 spin_unlock_irqrestore(&zr->spinlock, flags);
2545 case ZORAN_MAP_MODE_JPG_REC:
2546 case ZORAN_MAP_MODE_JPG_PLAY:
2547 poll_wait(file, &zr->jpg_capq, wait);
2548 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2550 spin_lock_irqsave(&zr->spinlock, flags);
2553 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
2554 ZR_DEVNAME(zr), __func__,
2555 "FAL"[fh->buffers.active], zr->jpg_que_tail,
2556 "UPMD"[zr->jpg_buffers.buffer[frame].state],
2557 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
2558 if (fh->buffers.active != ZORAN_FREE &&
2559 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
2560 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
2561 res |= EPOLLIN | EPOLLRDNORM;
2563 res |= EPOLLOUT | EPOLLWRNORM;
2565 spin_unlock_irqrestore(&zr->spinlock, flags);
2572 "%s: %s - internal error, unknown map_mode=%d\n",
2573 ZR_DEVNAME(zr), __func__, fh->map_mode);
2582 * This maps the buffers to user space.
2584 * Depending on the state of fh->map_mode
2585 * the V4L or the MJPEG buffers are mapped
2586 * per buffer or all together
2588 * Note that we need to connect to some
2589 * unmap signal event to unmap the de-allocate
2590 * the buffer accordingly (zoran_vm_close())
2594 zoran_vm_open (struct vm_area_struct *vma)
2596 struct zoran_mapping *map = vma->vm_private_data;
2597 atomic_inc(&map->count);
2601 zoran_vm_close (struct vm_area_struct *vma)
2603 struct zoran_mapping *map = vma->vm_private_data;
2604 struct zoran_fh *fh = map->fh;
2605 struct zoran *zr = fh->zr;
2608 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
2609 __func__, mode_name(fh->map_mode));
2611 for (i = 0; i < fh->buffers.num_buffers; i++) {
2612 if (fh->buffers.buffer[i].map == map)
2613 fh->buffers.buffer[i].map = NULL;
2617 /* Any buffers still mapped? */
2618 for (i = 0; i < fh->buffers.num_buffers; i++) {
2619 if (fh->buffers.buffer[i].map) {
2624 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
2625 __func__, mode_name(fh->map_mode));
2627 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2628 if (fh->buffers.active != ZORAN_FREE) {
2629 unsigned long flags;
2631 spin_lock_irqsave(&zr->spinlock, flags);
2632 zr36057_set_memgrab(zr, 0);
2633 zr->v4l_buffers.allocated = 0;
2634 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
2635 spin_unlock_irqrestore(&zr->spinlock, flags);
2637 v4l_fbuffer_free(fh);
2639 if (fh->buffers.active != ZORAN_FREE) {
2640 jpg_qbuf(fh, -1, zr->codec_mode);
2641 zr->jpg_buffers.allocated = 0;
2642 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
2644 jpg_fbuffer_free(fh);
2648 static const struct vm_operations_struct zoran_vm_ops = {
2649 .open = zoran_vm_open,
2650 .close = zoran_vm_close,
2654 zoran_mmap (struct file *file,
2655 struct vm_area_struct *vma)
2657 struct zoran_fh *fh = file->private_data;
2658 struct zoran *zr = fh->zr;
2659 unsigned long size = (vma->vm_end - vma->vm_start);
2660 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
2662 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
2664 struct zoran_mapping *map;
2668 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
2669 ZR_DEVNAME(zr), __func__,
2670 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
2672 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
2673 !(vma->vm_flags & VM_WRITE)) {
2676 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
2677 ZR_DEVNAME(zr), __func__);
2681 if (!fh->buffers.allocated) {
2684 "%s: %s(%s) - buffers not yet allocated\n",
2685 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
2690 first = offset / fh->buffers.buffer_size;
2691 last = first - 1 + size / fh->buffers.buffer_size;
2692 if (offset % fh->buffers.buffer_size != 0 ||
2693 size % fh->buffers.buffer_size != 0 || first < 0 ||
2694 last < 0 || first >= fh->buffers.num_buffers ||
2695 last >= fh->buffers.buffer_size) {
2698 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
2699 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
2700 fh->buffers.buffer_size,
2701 fh->buffers.num_buffers);
2706 /* Check if any buffers are already mapped */
2707 for (i = first; i <= last; i++) {
2708 if (fh->buffers.buffer[i].map) {
2711 "%s: %s(%s) - buffer %d already mapped\n",
2712 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
2718 /* map these buffers */
2719 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
2725 atomic_set(&map->count, 1);
2727 vma->vm_ops = &zoran_vm_ops;
2728 vma->vm_flags |= VM_DONTEXPAND;
2729 vma->vm_private_data = map;
2731 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
2732 for (i = first; i <= last; i++) {
2734 if (todo > fh->buffers.buffer_size)
2735 todo = fh->buffers.buffer_size;
2736 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
2737 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
2738 todo, PAGE_SHARED)) {
2741 "%s: %s(V4L) - remap_pfn_range failed\n",
2742 ZR_DEVNAME(zr), __func__);
2748 fh->buffers.buffer[i].map = map;
2753 for (i = first; i <= last; i++) {
2755 j < fh->buffers.buffer_size / PAGE_SIZE;
2758 (le32_to_cpu(fh->buffers.buffer[i].jpg.
2759 frag_tab[2 * j + 1]) & ~1) << 1;
2764 le32_to_cpu(fh->buffers.
2765 buffer[i].jpg.frag_tab[2 * j]);
2766 /* should just be pos on i386 */
2767 page = virt_to_phys(bus_to_virt(pos))
2769 if (remap_pfn_range(vma, start, page,
2770 todo, PAGE_SHARED)) {
2773 "%s: %s(V4L) - remap_pfn_range failed\n",
2774 ZR_DEVNAME(zr), __func__);
2782 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
2783 frag_tab[2 * j + 1]) & 1)
2784 break; /* was last fragment */
2786 fh->buffers.buffer[i].map = map;
2795 static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
2796 .vidioc_querycap = zoran_querycap,
2797 .vidioc_s_selection = zoran_s_selection,
2798 .vidioc_g_selection = zoran_g_selection,
2799 .vidioc_enum_input = zoran_enum_input,
2800 .vidioc_g_input = zoran_g_input,
2801 .vidioc_s_input = zoran_s_input,
2802 .vidioc_enum_output = zoran_enum_output,
2803 .vidioc_g_output = zoran_g_output,
2804 .vidioc_s_output = zoran_s_output,
2805 .vidioc_g_fbuf = zoran_g_fbuf,
2806 .vidioc_s_fbuf = zoran_s_fbuf,
2807 .vidioc_g_std = zoran_g_std,
2808 .vidioc_s_std = zoran_s_std,
2809 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
2810 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
2811 .vidioc_overlay = zoran_overlay,
2812 .vidioc_reqbufs = zoran_reqbufs,
2813 .vidioc_querybuf = zoran_querybuf,
2814 .vidioc_qbuf = zoran_qbuf,
2815 .vidioc_dqbuf = zoran_dqbuf,
2816 .vidioc_streamon = zoran_streamon,
2817 .vidioc_streamoff = zoran_streamoff,
2818 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
2819 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
2820 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
2821 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
2822 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
2823 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
2824 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
2825 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
2826 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
2827 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
2828 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
2829 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
2830 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2831 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2834 static const struct v4l2_file_operations zoran_fops = {
2835 .owner = THIS_MODULE,
2837 .release = zoran_close,
2838 .unlocked_ioctl = video_ioctl2,
2843 const struct video_device zoran_template = {
2845 .fops = &zoran_fops,
2846 .ioctl_ops = &zoran_ioctl_ops,
2847 .release = &zoran_vdev_release,
2848 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,