blob: bcdefb1bcb3dc450a54b0c05fb9e58c7dd91c81b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
5 *
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 *
8 * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
9 *
10 * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
11 *
12 * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
13 *
14 * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
15 *
16 * Based on
17 *
18 * Miro DC10 driver
19 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
20 *
21 * Iomega Buz driver version 1.0
22 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
23 *
24 * buz.0.0.3
25 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
26 *
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)
30 *
31 *
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.
36 *
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.
41 *
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.
45 */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/version.h>
48#include <linux/init.h>
49#include <linux/module.h>
50#include <linux/delay.h>
51#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040052#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/pci.h>
54#include <linux/vmalloc.h>
55#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <linux/interrupt.h>
58#include <linux/i2c.h>
59#include <linux/i2c-algo-bit.h>
60
61#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mauro Carvalho Chehab7e0a16f2009-03-10 05:31:34 -030063#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030064#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030065#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include "videocodec.h"
67
Harvey Harrison9a6ab762008-05-16 11:20:25 -070068#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/io.h>
70#include <asm/uaccess.h>
71#include <linux/proc_fs.h>
72
Ingo Molnar384c3682006-03-22 03:54:16 -030073#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "zoran.h"
75#include "zoran_device.h"
76#include "zoran_card.h"
77
Trent Piepho603d6f22007-07-17 18:29:44 -030078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079const struct zoran_format zoran_formats[] = {
80 {
Trent Piepho603d6f22007-07-17 18:29:44 -030081 .name = "15-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -030082 .fourcc = V4L2_PIX_FMT_RGB555,
83 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .depth = 15,
85 .flags = ZORAN_FORMAT_CAPTURE |
86 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -030087 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
88 ZR36057_VFESPFR_LittleEndian,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }, {
Trent Piepho603d6f22007-07-17 18:29:44 -030090 .name = "15-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -030091 .fourcc = V4L2_PIX_FMT_RGB555X,
92 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -030093 .depth = 15,
94 .flags = ZORAN_FORMAT_CAPTURE |
95 ZORAN_FORMAT_OVERLAY,
96 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
97 }, {
98 .name = "16-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -030099 .fourcc = V4L2_PIX_FMT_RGB565,
100 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .depth = 16,
102 .flags = ZORAN_FORMAT_CAPTURE |
103 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300104 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
105 ZR36057_VFESPFR_LittleEndian,
106 }, {
107 .name = "16-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300108 .fourcc = V4L2_PIX_FMT_RGB565X,
109 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -0300110 .depth = 16,
111 .flags = ZORAN_FORMAT_CAPTURE |
112 ZORAN_FORMAT_OVERLAY,
113 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }, {
115 .name = "24-bit RGB",
Hans Verkuil602dd482009-02-18 17:39:45 -0300116 .fourcc = V4L2_PIX_FMT_BGR24,
117 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .depth = 24,
119 .flags = ZORAN_FORMAT_CAPTURE |
120 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300121 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }, {
Trent Piepho603d6f22007-07-17 18:29:44 -0300123 .name = "32-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300124 .fourcc = V4L2_PIX_FMT_BGR32,
125 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .depth = 32,
127 .flags = ZORAN_FORMAT_CAPTURE |
128 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300129 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
130 }, {
131 .name = "32-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300132 .fourcc = V4L2_PIX_FMT_RGB32,
133 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -0300134 .depth = 32,
135 .flags = ZORAN_FORMAT_CAPTURE |
136 ZORAN_FORMAT_OVERLAY,
137 .vfespfr = ZR36057_VFESPFR_RGB888,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }, {
139 .name = "4:2:2, packed, YUYV",
Hans Verkuil602dd482009-02-18 17:39:45 -0300140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .depth = 16,
143 .flags = ZORAN_FORMAT_CAPTURE |
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300144 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300145 .vfespfr = ZR36057_VFESPFR_YUV422,
146 }, {
147 .name = "4:2:2, packed, UYVY",
Hans Verkuil602dd482009-02-18 17:39:45 -0300148 .fourcc = V4L2_PIX_FMT_UYVY,
149 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Trent Piepho603d6f22007-07-17 18:29:44 -0300150 .depth = 16,
151 .flags = ZORAN_FORMAT_CAPTURE |
152 ZORAN_FORMAT_OVERLAY,
153 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }, {
155 .name = "Hardware-encoded Motion-JPEG",
Hans Verkuil602dd482009-02-18 17:39:45 -0300156 .fourcc = V4L2_PIX_FMT_MJPEG,
157 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .depth = 0,
159 .flags = ZORAN_FORMAT_CAPTURE |
160 ZORAN_FORMAT_PLAYBACK |
161 ZORAN_FORMAT_COMPRESSED,
162 }
163};
Trent Piepho603d6f22007-07-17 18:29:44 -0300164#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* small helper function for calculating buffersizes for v4l2
167 * we calculate the nearest higher power-of-two, which
168 * will be the recommended buffersize */
169static __u32
170zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
171{
172 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
173 __u32 num = (1024 * 512) / (div);
174 __u32 result = 2;
175
176 num--;
177 while (num) {
178 num >>= 1;
179 result <<= 1;
180 }
181
182 if (result > jpg_bufsize)
183 return jpg_bufsize;
184 if (result < 8192)
185 return 8192;
186 return result;
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/* forward references */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300190static void v4l_fbuffer_free(struct zoran_fh *fh);
191static void jpg_fbuffer_free(struct zoran_fh *fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Trent Piepho1159b7f2009-03-10 23:28:16 -0300193/* Set mapping mode */
194static void map_mode_raw(struct zoran_fh *fh)
195{
196 fh->map_mode = ZORAN_MAP_MODE_RAW;
197 fh->buffers.buffer_size = v4l_bufsize;
198 fh->buffers.num_buffers = v4l_nbufs;
199}
200static void map_mode_jpg(struct zoran_fh *fh, int play)
201{
202 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
203 fh->buffers.buffer_size = jpg_bufsize;
204 fh->buffers.num_buffers = jpg_nbufs;
205}
206static inline const char *mode_name(enum zoran_map_mode mode)
207{
208 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Allocate the V4L grab buffers
213 *
214 * These have to be pysically contiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
216
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300217static int v4l_fbuffer_alloc(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct zoran *zr = fh->zr;
220 int i, off;
221 unsigned char *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Trent Piepho1159b7f2009-03-10 23:28:16 -0300223 for (i = 0; i < fh->buffers.num_buffers; i++) {
224 if (fh->buffers.buffer[i].v4l.fbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dprintk(2,
226 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300227 "%s: %s - buffer %d already allocated!?\n",
228 ZR_DEVNAME(zr), __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 //udelay(20);
Jean Delvareee9a9d62009-03-10 23:28:20 -0300231 mem = kmalloc(fh->buffers.buffer_size,
232 GFP_KERNEL | __GFP_NOWARN);
Hans Verkuilb80696b2009-02-18 13:20:05 -0300233 if (!mem) {
234 dprintk(1,
235 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300236 "%s: %s - kmalloc for V4L buf %d failed\n",
237 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300238 v4l_fbuffer_free(fh);
Hans Verkuilb80696b2009-02-18 13:20:05 -0300239 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300241 fh->buffers.buffer[i].v4l.fbuffer = mem;
242 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
243 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
244 for (off = 0; off < fh->buffers.buffer_size;
Hans Verkuilb80696b2009-02-18 13:20:05 -0300245 off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300246 SetPageReserved(virt_to_page(mem + off));
Hans Verkuilb80696b2009-02-18 13:20:05 -0300247 dprintk(4,
248 KERN_INFO
Randy Dunlap8737f662009-03-27 14:01:11 -0300249 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
Trent Piephoc61402b2009-03-10 23:28:33 -0300250 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
Randy Dunlap8737f662009-03-27 14:01:11 -0300251 (unsigned long long)virt_to_bus(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Trent Piepho1159b7f2009-03-10 23:28:16 -0300254 fh->buffers.allocated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return 0;
257}
258
259/* free the V4L grab buffers */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300260static void v4l_fbuffer_free(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct zoran *zr = fh->zr;
263 int i, off;
264 unsigned char *mem;
265
Trent Piephoc61402b2009-03-10 23:28:33 -0300266 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Trent Piepho1159b7f2009-03-10 23:28:16 -0300268 for (i = 0; i < fh->buffers.num_buffers; i++) {
269 if (!fh->buffers.buffer[i].v4l.fbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 continue;
271
Trent Piepho1159b7f2009-03-10 23:28:16 -0300272 mem = fh->buffers.buffer[i].v4l.fbuffer;
273 for (off = 0; off < fh->buffers.buffer_size;
Hans Verkuilb80696b2009-02-18 13:20:05 -0300274 off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300275 ClearPageReserved(virt_to_page(mem + off));
Trent Piepho1159b7f2009-03-10 23:28:16 -0300276 kfree(fh->buffers.buffer[i].v4l.fbuffer);
277 fh->buffers.buffer[i].v4l.fbuffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Trent Piepho1159b7f2009-03-10 23:28:16 -0300280 fh->buffers.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/*
284 * Allocate the MJPEG grab buffers.
285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * If a Natoma chipset is present and this is a revision 1 zr36057,
287 * each MJPEG buffer needs to be physically contiguous.
288 * (RJ: This statement is from Dave Perks' original driver,
289 * I could never check it because I have a zr36067)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
291 * RJ: The contents grab buffers needs never be accessed in the driver.
292 * Therefore there is no need to allocate them with vmalloc in order
293 * to get a contiguous virtual memory space.
294 * I don't understand why many other drivers first allocate them with
295 * vmalloc (which uses internally also get_zeroed_page, but delivers you
296 * virtual addresses) and then again have to make a lot of efforts
297 * to get the physical address.
298 *
299 * Ben Capper:
300 * On big-endian architectures (such as ppc) some extra steps
301 * are needed. When reading and writing to the stat_com array
302 * and fragment buffers, the device expects to see little-
303 * endian values. The use of cpu_to_le32() and le32_to_cpu()
304 * in this function (and one or two others in zoran_device.c)
305 * ensure that these values are always stored in little-endian
306 * form, regardless of architecture. The zr36057 does Very Bad
307 * Things on big endian architectures if the stat_com array
308 * and fragment buffers are not little-endian.
309 */
310
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300311static int jpg_fbuffer_alloc(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct zoran *zr = fh->zr;
314 int i, j, off;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300315 u8 *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Trent Piepho1159b7f2009-03-10 23:28:16 -0300317 for (i = 0; i < fh->buffers.num_buffers; i++) {
318 if (fh->buffers.buffer[i].jpg.frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 dprintk(2,
320 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300321 "%s: %s - buffer %d already allocated!?\n",
322 ZR_DEVNAME(zr), __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Allocate fragment table for this buffer */
325
Trent Piepho1159b7f2009-03-10 23:28:16 -0300326 mem = (void *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (mem == 0) {
328 dprintk(1,
329 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300330 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
331 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300332 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -ENOBUFS;
334 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300335 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
336 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Trent Piepho1159b7f2009-03-10 23:28:16 -0300338 if (fh->buffers.need_contiguous) {
339 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
340 if (mem == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dprintk(1,
342 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300343 "%s: %s - kmalloc failed for buffer %d\n",
344 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300345 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -ENOBUFS;
347 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300348 fh->buffers.buffer[i].jpg.frag_tab[0] =
349 cpu_to_le32(virt_to_bus(mem));
350 fh->buffers.buffer[i].jpg.frag_tab[1] =
351 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
352 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300353 SetPageReserved(virt_to_page(mem + off));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 } else {
Hans Verkuilb80696b2009-02-18 13:20:05 -0300355 /* jpg_bufsize is already page aligned */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300356 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
357 mem = (void *)get_zeroed_page(GFP_KERNEL);
358 if (mem == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dprintk(1,
360 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300361 "%s: %s - get_zeroed_page failed for buffer %d\n",
362 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300363 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -ENOBUFS;
365 }
366
Trent Piepho1159b7f2009-03-10 23:28:16 -0300367 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
368 cpu_to_le32(virt_to_bus(mem));
369 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
370 cpu_to_le32((PAGE_SIZE >> 2) << 1);
Hans Verkuil602dd482009-02-18 17:39:45 -0300371 SetPageReserved(virt_to_page(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Trent Piepho1159b7f2009-03-10 23:28:16 -0300374 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 }
377
378 dprintk(4,
Trent Piephoc61402b2009-03-10 23:28:33 -0300379 KERN_DEBUG "%s: %s - %d KB allocated\n",
380 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -0300381 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Trent Piepho1159b7f2009-03-10 23:28:16 -0300383 fh->buffers.allocated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
388/* free the MJPEG grab buffers */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300389static void jpg_fbuffer_free(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct zoran *zr = fh->zr;
392 int i, j, off;
393 unsigned char *mem;
Hans Verkuil602dd482009-02-18 17:39:45 -0300394 __le32 frag_tab;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300395 struct zoran_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Trent Piephoc61402b2009-03-10 23:28:33 -0300397 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Trent Piepho1159b7f2009-03-10 23:28:16 -0300399 for (i = 0, buffer = &fh->buffers.buffer[0];
400 i < fh->buffers.num_buffers; i++, buffer++) {
401 if (!buffer->jpg.frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 continue;
403
Trent Piepho1159b7f2009-03-10 23:28:16 -0300404 if (fh->buffers.need_contiguous) {
405 frag_tab = buffer->jpg.frag_tab[0];
Hans Verkuil602dd482009-02-18 17:39:45 -0300406
407 if (frag_tab) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300408 mem = bus_to_virt(le32_to_cpu(frag_tab));
409 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300410 ClearPageReserved(virt_to_page(mem + off));
Jesper Juhlf91012102005-09-10 00:26:54 -0700411 kfree(mem);
Trent Piepho1159b7f2009-03-10 23:28:16 -0300412 buffer->jpg.frag_tab[0] = 0;
413 buffer->jpg.frag_tab[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 } else {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300416 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
417 frag_tab = buffer->jpg.frag_tab[2 * j];
Hans Verkuil602dd482009-02-18 17:39:45 -0300418
419 if (!frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
Hans Verkuil602dd482009-02-18 17:39:45 -0300421 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
422 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
Trent Piepho1159b7f2009-03-10 23:28:16 -0300423 buffer->jpg.frag_tab[2 * j] = 0;
424 buffer->jpg.frag_tab[2 * j + 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 }
427
Trent Piepho1159b7f2009-03-10 23:28:16 -0300428 free_page((unsigned long)buffer->jpg.frag_tab);
429 buffer->jpg.frag_tab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
Trent Piepho1159b7f2009-03-10 23:28:16 -0300432 fh->buffers.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/*
436 * V4L Buffer grabbing
437 */
438
439static int
Trent Piepho1159b7f2009-03-10 23:28:16 -0300440zoran_v4l_set_format (struct zoran_fh *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int width,
442 int height,
443 const struct zoran_format *format)
444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct zoran *zr = fh->zr;
446 int bpp;
447
448 /* Check size and format of the grab wanted */
449
450 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
451 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
452 dprintk(1,
453 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300454 "%s: %s - wrong frame size (%dx%d)\n",
455 ZR_DEVNAME(zr), __func__, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return -EINVAL;
457 }
458
459 bpp = (format->depth + 7) / 8;
460
461 /* Check against available buffer size */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300462 if (height * width * bpp > fh->buffers.buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 dprintk(1,
464 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300465 "%s: %s - video buffer size (%d kB) is too small\n",
466 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
468 }
469
470 /* The video front end needs 4-byte alinged line sizes */
471
472 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
473 dprintk(1,
474 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300475 "%s: %s - wrong frame alignment\n",
476 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EINVAL;
478 }
479
480 fh->v4l_settings.width = width;
481 fh->v4l_settings.height = height;
482 fh->v4l_settings.format = format;
483 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
484
485 return 0;
486}
487
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300488static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct zoran *zr = fh->zr;
491 unsigned long flags;
492 int res = 0;
493
Trent Piepho1159b7f2009-03-10 23:28:16 -0300494 if (!fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dprintk(1,
496 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300497 "%s: %s - buffers not yet allocated\n",
498 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 res = -ENOMEM;
500 }
501
502 /* No grabbing outside the buffer range! */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300503 if (num >= fh->buffers.num_buffers || num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 dprintk(1,
505 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300506 "%s: %s - buffer %d is out of range\n",
507 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 res = -EINVAL;
509 }
510
511 spin_lock_irqsave(&zr->spinlock, flags);
512
Trent Piepho1159b7f2009-03-10 23:28:16 -0300513 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (zr->v4l_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300515 zr->v4l_buffers = fh->buffers;
516 fh->buffers.active = ZORAN_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 } else {
518 dprintk(1,
519 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300520 "%s: %s - another session is already capturing\n",
521 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 res = -EBUSY;
523 }
524 }
525
526 /* make sure a grab isn't going on currently with this buffer */
527 if (!res) {
528 switch (zr->v4l_buffers.buffer[num].state) {
529 default:
530 case BUZ_STATE_PEND:
531 if (zr->v4l_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300532 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 zr->v4l_buffers.allocated = 0;
534 }
535 res = -EBUSY; /* what are you doing? */
536 break;
537 case BUZ_STATE_DONE:
538 dprintk(2,
539 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300540 "%s: %s - queueing buffer %d in state DONE!?\n",
541 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 case BUZ_STATE_USER:
543 /* since there is at least one unused buffer there's room for at least
544 * one more pend[] entry */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300545 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
547 zr->v4l_buffers.buffer[num].bs.length =
548 fh->v4l_settings.bytesperline *
549 zr->v4l_settings.height;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300550 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552 }
553 }
554
555 spin_unlock_irqrestore(&zr->spinlock, flags);
556
557 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
Trent Piepho1159b7f2009-03-10 23:28:16 -0300558 zr->v4l_buffers.active = fh->buffers.active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return res;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Sync on a V4L buffer
565 */
566
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300567static int v4l_sync(struct zoran_fh *fh, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct zoran *zr = fh->zr;
570 unsigned long flags;
571
Trent Piepho1159b7f2009-03-10 23:28:16 -0300572 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dprintk(1,
574 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300575 "%s: %s - no grab active for this session\n",
576 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -EINVAL;
578 }
579
580 /* check passed-in frame number */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300581 if (frame >= fh->buffers.num_buffers || frame < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dprintk(1,
Trent Piephoc61402b2009-03-10 23:28:33 -0300583 KERN_ERR "%s: %s - frame %d is invalid\n",
584 ZR_DEVNAME(zr), __func__, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EINVAL;
586 }
587
588 /* Check if is buffer was queued at all */
589 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
590 dprintk(1,
591 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300592 "%s: %s - attempt to sync on a buffer which was not queued?\n",
593 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -EPROTO;
595 }
596
597 /* wait on this buffer to get ready */
598 if (!wait_event_interruptible_timeout(zr->v4l_capq,
Trent Piepho1159b7f2009-03-10 23:28:16 -0300599 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -ETIME;
601 if (signal_pending(current))
602 return -ERESTARTSYS;
603
604 /* buffer should now be in BUZ_STATE_DONE */
605 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
606 dprintk(2,
Trent Piephoc61402b2009-03-10 23:28:33 -0300607 KERN_ERR "%s: %s - internal state error\n",
608 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300611 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 spin_lock_irqsave(&zr->spinlock, flags);
614
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) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300619 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 zr->v4l_buffers.allocated = 0;
621 }
622 }
623
624 spin_unlock_irqrestore(&zr->spinlock, flags);
625
626 return 0;
627}
628
629/*
630 * Queue a MJPEG buffer for capture/playback
631 */
632
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300633static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
634 enum zoran_codec_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct zoran *zr = fh->zr;
637 unsigned long flags;
638 int res = 0;
639
640 /* Check if buffers are allocated */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300641 if (!fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 dprintk(1,
643 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300644 "%s: %s - buffers not yet allocated\n",
645 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -ENOMEM;
647 }
648
649 /* No grabbing outside the buffer range! */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300650 if (num >= fh->buffers.num_buffers || num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dprintk(1,
652 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300653 "%s: %s - buffer %d out of range\n",
654 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EINVAL;
656 }
657
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 */
663 dprintk(1,
664 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300665 "%s: %s - codec in wrong mode\n",
666 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -EINVAL;
668 }
669
Trent Piepho1159b7f2009-03-10 23:28:16 -0300670 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (zr->jpg_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300672 zr->jpg_buffers = fh->buffers;
673 fh->buffers.active = ZORAN_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 } else {
675 dprintk(1,
676 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300677 "%s: %s - another session is already capturing\n",
678 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 res = -EBUSY;
680 }
681 }
682
683 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
684 /* Ok load up the jpeg codec */
685 zr36057_enable_jpg(zr, mode);
686 }
687
Ronald S. Bultje8bc3efc2005-11-07 01:00:22 -0800688 spin_lock_irqsave(&zr->spinlock, flags);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!res) {
691 switch (zr->jpg_buffers.buffer[num].state) {
692 case BUZ_STATE_DONE:
693 dprintk(2,
694 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300695 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
696 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 case BUZ_STATE_USER:
698 /* since there is at least one unused buffer there's room for at
699 *least one more pend[] entry */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300700 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300702 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 zoran_feed_stat_com(zr);
704 break;
705 default:
706 case BUZ_STATE_DMA:
707 case BUZ_STATE_PEND:
708 if (zr->jpg_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300709 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 zr->jpg_buffers.allocated = 0;
711 }
712 res = -EBUSY; /* what are you doing? */
713 break;
714 }
715 }
716
717 spin_unlock_irqrestore(&zr->spinlock, flags);
718
Trent Piepho1159b7f2009-03-10 23:28:16 -0300719 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
720 zr->jpg_buffers.active = fh->buffers.active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 return res;
723}
724
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300725static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct zoran *zr = fh->zr;
728 int res = 0;
729
730 /* Does the user want to stop streaming? */
731 if (frame < 0) {
732 if (zr->codec_mode == mode) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300733 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dprintk(1,
735 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300736 "%s: %s(-1) - session not active\n",
737 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
739 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300740 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 zr->jpg_buffers.allocated = 0;
742 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
743 return 0;
744 } else {
745 dprintk(1,
746 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300747 "%s: %s - stop streaming but not in streaming mode\n",
748 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -EINVAL;
750 }
751 }
752
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300753 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return res;
755
756 /* Start the jpeg codec when the first frame is queued */
757 if (!res && zr->jpg_que_head == 1)
758 jpeg_start(zr);
759
760 return res;
761}
762
763/*
764 * Sync on a MJPEG buffer
765 */
766
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300767static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct zoran *zr = fh->zr;
770 unsigned long flags;
771 int frame;
772
Trent Piepho1159b7f2009-03-10 23:28:16 -0300773 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 dprintk(1,
775 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300776 "%s: %s - capture is not currently active\n",
777 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EINVAL;
779 }
780 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
781 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
782 dprintk(1,
783 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300784 "%s: %s - codec not in streaming mode\n",
785 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EINVAL;
787 }
788 if (!wait_event_interruptible_timeout(zr->jpg_capq,
789 (zr->jpg_que_tail != zr->jpg_dma_tail ||
790 zr->jpg_dma_tail == zr->jpg_dma_head),
791 10*HZ)) {
792 int isr;
793
794 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
795 udelay(1);
796 zr->codec->control(zr->codec, CODEC_G_STATUS,
797 sizeof(isr), &isr);
798 dprintk(1,
799 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300800 "%s: %s - timeout: codec isr=0x%02x\n",
801 ZR_DEVNAME(zr), __func__, isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 return -ETIME;
804
805 }
806 if (signal_pending(current))
807 return -ERESTARTSYS;
808
809 spin_lock_irqsave(&zr->spinlock, flags);
810
811 if (zr->jpg_dma_tail != zr->jpg_dma_head)
812 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
813 else
814 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
815
816 /* buffer should now be in BUZ_STATE_DONE */
Jean Delvare18b548c2007-07-17 18:29:41 -0300817 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
818 dprintk(2,
Trent Piephoc61402b2009-03-10 23:28:33 -0300819 KERN_ERR "%s: %s - internal state error\n",
820 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 *bs = zr->jpg_buffers.buffer[frame].bs;
823 bs->frame = frame;
824 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300825 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 spin_unlock_irqrestore(&zr->spinlock, flags);
828
829 return 0;
830}
831
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300832static void zoran_open_init_session(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct zoran *zr = fh->zr;
836
837 /* Per default, map the V4L Buffers */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300838 map_mode_raw(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* take over the card's current settings */
841 fh->overlay_settings = zr->overlay_settings;
842 fh->overlay_settings.is_set = 0;
843 fh->overlay_settings.format = zr->overlay_settings.format;
844 fh->overlay_active = ZORAN_FREE;
845
846 /* v4l settings */
847 fh->v4l_settings = zr->v4l_settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* jpg settings */
849 fh->jpg_settings = zr->jpg_settings;
850
Trent Piepho1159b7f2009-03-10 23:28:16 -0300851 /* buffers */
852 memset(&fh->buffers, 0, sizeof(fh->buffers));
853 for (i = 0; i < MAX_FRAME; i++) {
854 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
855 fh->buffers.buffer[i].bs.frame = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300857 fh->buffers.allocated = 0;
858 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300861static void zoran_close_end_session(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct zoran *zr = fh->zr;
864
865 /* overlay */
866 if (fh->overlay_active != ZORAN_FREE) {
867 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
868 zr->v4l_overlay_active = 0;
869 if (!zr->v4l_memgrab_active)
870 zr36057_overlay(zr, 0);
871 zr->overlay_mask = NULL;
872 }
873
Trent Piepho1159b7f2009-03-10 23:28:16 -0300874 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
875 /* v4l capture */
876 if (fh->buffers.active != ZORAN_FREE) {
877 unsigned long flags;
Trent Piepho9896bbc2007-07-17 18:29:45 -0300878
Trent Piepho1159b7f2009-03-10 23:28:16 -0300879 spin_lock_irqsave(&zr->spinlock, flags);
880 zr36057_set_memgrab(zr, 0);
881 zr->v4l_buffers.allocated = 0;
882 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
883 spin_unlock_irqrestore(&zr->spinlock, flags);
884 }
885
886 /* v4l buffers */
887 if (fh->buffers.allocated)
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300888 v4l_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -0300889 } else {
890 /* jpg capture */
891 if (fh->buffers.active != ZORAN_FREE) {
892 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
893 zr->jpg_buffers.allocated = 0;
894 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
895 }
896
897 /* jpg buffers */
898 if (fh->buffers.allocated)
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300899 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903/*
904 * Open a zoran card. Right now the flags stuff is just playing
905 */
906
Trent Piepho601139e2009-01-12 13:09:46 -0300907static int zoran_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Trent Piepho601139e2009-01-12 13:09:46 -0300909 struct zoran *zr = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct zoran_fh *fh;
Trent Piephofc96ab72009-01-12 13:09:46 -0300911 int res, first_open = 0;
912
Trent Piephoc61402b2009-03-10 23:28:33 -0300913 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
914 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Hans Verkuild56dc612008-07-30 08:43:36 -0300916 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Trent Piephofc96ab72009-01-12 13:09:46 -0300918 if (zr->user >= 2048) {
919 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
920 ZR_DEVNAME(zr), zr->user);
921 res = -EBUSY;
922 goto fail_unlock;
923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* now, create the open()-specific file_ops struct */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200926 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!fh) {
928 dprintk(1,
929 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300930 "%s: %s - allocation of zoran_fh failed\n",
931 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 res = -ENOMEM;
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300933 goto fail_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
936 * on norm-change! */
937 fh->overlay_mask =
938 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
939 if (!fh->overlay_mask) {
940 dprintk(1,
941 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300942 "%s: %s - allocation of overlay_mask failed\n",
943 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 res = -ENOMEM;
Trent Piephofc96ab72009-01-12 13:09:46 -0300945 goto fail_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947
948 if (zr->user++ == 0)
949 first_open = 1;
950
Ingo Molnar384c3682006-03-22 03:54:16 -0300951 /*mutex_unlock(&zr->resource_lock);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* default setup - TODO: look at flags */
954 if (first_open) { /* First device open */
955 zr36057_restart(zr);
956 zoran_open_init_params(zr);
957 zoran_init_hardware(zr);
958
959 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
960 }
961
962 /* set file_ops stuff */
963 file->private_data = fh;
964 fh->zr = zr;
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300965 zoran_open_init_session(fh);
Hans Verkuild56dc612008-07-30 08:43:36 -0300966 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 return 0;
969
Trent Piephofc96ab72009-01-12 13:09:46 -0300970fail_fh:
971 kfree(fh);
Trent Piephofc96ab72009-01-12 13:09:46 -0300972fail_unlock:
Hans Verkuild56dc612008-07-30 08:43:36 -0300973 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Trent Piephofc96ab72009-01-12 13:09:46 -0300975 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
976 ZR_DEVNAME(zr), res, zr->user);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return res;
979}
980
981static int
Hans Verkuilbec43662008-12-30 06:58:20 -0300982zoran_close(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct zoran_fh *fh = file->private_data;
985 struct zoran *zr = fh->zr;
986
Trent Piephoc61402b2009-03-10 23:28:33 -0300987 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
988 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* kernel locks (fs/device.c), so don't do that ourselves
991 * (prevents deadlocks) */
Ingo Molnar384c3682006-03-22 03:54:16 -0300992 /*mutex_lock(&zr->resource_lock);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300994 zoran_close_end_session(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (zr->user-- == 1) { /* Last process */
997 /* Clean up JPEG process */
998 wake_up_interruptible(&zr->jpg_capq);
999 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1000 zr->jpg_buffers.allocated = 0;
1001 zr->jpg_buffers.active = ZORAN_FREE;
1002
1003 /* disable interrupts */
1004 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1005
Jean Delvare18b548c2007-07-17 18:29:41 -03001006 if (zr36067_debug > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 print_interrupts(zr);
1008
1009 /* Overlay off */
1010 zr->v4l_overlay_active = 0;
1011 zr36057_overlay(zr, 0);
1012 zr->overlay_mask = NULL;
1013
1014 /* capture off */
1015 wake_up_interruptible(&zr->v4l_capq);
1016 zr36057_set_memgrab(zr, 0);
1017 zr->v4l_buffers.allocated = 0;
1018 zr->v4l_buffers.active = ZORAN_FREE;
1019 zoran_set_pci_master(zr, 0);
1020
1021 if (!pass_through) { /* Switch to color bar */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001022 decoder_call(zr, video, s_stream, 0);
Hans Verkuil5325b422009-04-02 11:26:22 -03001023 encoder_call(zr, video, s_routing, 2, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
1026
1027 file->private_data = NULL;
1028 kfree(fh->overlay_mask);
1029 kfree(fh);
1030
Trent Piephoc61402b2009-03-10 23:28:33 -03001031 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 return 0;
1034}
1035
1036
1037static ssize_t
1038zoran_read (struct file *file,
1039 char __user *data,
1040 size_t count,
1041 loff_t *ppos)
1042{
1043 /* we simply don't support read() (yet)... */
1044
1045 return -EINVAL;
1046}
1047
1048static ssize_t
1049zoran_write (struct file *file,
1050 const char __user *data,
1051 size_t count,
1052 loff_t *ppos)
1053{
1054 /* ...and the same goes for write() */
1055
1056 return -EINVAL;
1057}
1058
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001059static int setup_fbuffer(struct zoran_fh *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 void *base,
1061 const struct zoran_format *fmt,
1062 int width,
1063 int height,
1064 int bytesperline)
1065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct zoran *zr = fh->zr;
1067
1068 /* (Ronald) v4l/v4l2 guidelines */
1069 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1070 return -EPERM;
1071
Alan Coxe3558802006-09-14 11:47:55 -03001072 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1073 ALi Magik (that needs very low latency while the card needs a
1074 higher value always) */
1075
1076 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1077 return -ENXIO;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* we need a bytesperline value, even if not given */
1080 if (!bytesperline)
1081 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1082
1083#if 0
1084 if (zr->overlay_active) {
1085 /* dzjee... stupid users... don't even bother to turn off
1086 * overlay before changing the memory location...
1087 * normally, we would return errors here. However, one of
1088 * the tools that does this is... xawtv! and since xawtv
1089 * is used by +/- 99% of the users, we'd rather be user-
1090 * friendly and silently do as if nothing went wrong */
1091 dprintk(3,
1092 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001093 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1094 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 zr36057_overlay(zr, 0);
1096 }
1097#endif
1098
1099 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1100 dprintk(1,
1101 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001102 "%s: %s - no valid overlay format given\n",
1103 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EINVAL;
1105 }
1106 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1107 dprintk(1,
1108 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001109 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1110 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EINVAL;
1112 }
1113 if (bytesperline & 3) {
1114 dprintk(1,
1115 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001116 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1117 ZR_DEVNAME(zr), __func__, bytesperline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -EINVAL;
1119 }
1120
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001121 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1122 zr->vbuf_height = height;
1123 zr->vbuf_width = width;
1124 zr->vbuf_depth = fmt->depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 zr->overlay_settings.format = fmt;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001126 zr->vbuf_bytesperline = bytesperline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* The user should set new window parameters */
1129 zr->overlay_settings.is_set = 0;
1130
1131 return 0;
1132}
1133
1134
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001135static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1136 struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct zoran *zr = fh->zr;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001139 struct v4l2_clip *vcp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int on, end;
1141
1142
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001143 if (!zr->vbuf_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dprintk(1,
1145 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001146 "%s: %s - frame buffer has to be set first\n",
1147 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return -EINVAL;
1149 }
1150
1151 if (!fh->overlay_settings.format) {
1152 dprintk(1,
1153 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001154 "%s: %s - no overlay format set\n",
1155 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -EINVAL;
1157 }
1158
1159 /*
1160 * The video front end needs 4-byte alinged line sizes, we correct that
1161 * silently here if necessary
1162 */
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001163 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 end = (x + width) & ~1; /* round down */
1165 x = (x + 1) & ~1; /* round up */
1166 width = end - x;
1167 }
1168
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001169 if (zr->vbuf_depth == 24) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 end = (x + width) & ~3; /* round down */
1171 x = (x + 3) & ~3; /* round up */
1172 width = end - x;
1173 }
1174
1175 if (width > BUZ_MAX_WIDTH)
1176 width = BUZ_MAX_WIDTH;
1177 if (height > BUZ_MAX_HEIGHT)
1178 height = BUZ_MAX_HEIGHT;
1179
1180 /* Check for vaild parameters */
1181 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1182 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1183 dprintk(1,
1184 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001185 "%s: %s - width = %d or height = %d invalid\n",
1186 ZR_DEVNAME(zr), __func__, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EINVAL;
1188 }
1189
1190 fh->overlay_settings.x = x;
1191 fh->overlay_settings.y = y;
1192 fh->overlay_settings.width = width;
1193 fh->overlay_settings.height = height;
1194 fh->overlay_settings.clipcount = clipcount;
1195
1196 /*
1197 * If an overlay is running, we have to switch it off
1198 * and switch it on again in order to get the new settings in effect.
1199 *
1200 * We also want to avoid that the overlay mask is written
1201 * when an overlay is running.
1202 */
1203
1204 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1205 zr->overlay_active != ZORAN_FREE &&
1206 fh->overlay_active != ZORAN_FREE;
1207 if (on)
1208 zr36057_overlay(zr, 0);
1209
1210 /*
1211 * Write the overlay mask if clips are wanted.
1212 * We prefer a bitmap.
1213 */
1214 if (bitmap) {
1215 /* fake value - it just means we want clips */
1216 fh->overlay_settings.clipcount = 1;
1217
1218 if (copy_from_user(fh->overlay_mask, bitmap,
1219 (width * height + 7) / 8)) {
1220 return -EFAULT;
1221 }
1222 } else if (clipcount > 0) {
1223 /* write our own bitmap from the clips */
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001224 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (vcp == NULL) {
1226 dprintk(1,
1227 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001228 "%s: %s - Alloc of clip mask failed\n",
1229 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -ENOMEM;
1231 }
1232 if (copy_from_user
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001233 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 vfree(vcp);
1235 return -EFAULT;
1236 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001237 write_overlay_mask(fh, vcp, clipcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 vfree(vcp);
1239 }
1240
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;
1245
1246 if (on)
1247 zr36057_overlay(zr, 1);
1248
1249 /* Make sure the changes come into effect */
1250 return wait_grab_pending(zr);
1251}
1252
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001253static int setup_overlay(struct zoran_fh *fh, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct zoran *zr = fh->zr;
1256
1257 /* If there is nothing to do, return immediatly */
1258 if ((on && fh->overlay_active != ZORAN_FREE) ||
1259 (!on && fh->overlay_active == ZORAN_FREE))
1260 return 0;
1261
1262 /* check whether we're touching someone else's overlay */
1263 if (on && zr->overlay_active != ZORAN_FREE &&
1264 fh->overlay_active == ZORAN_FREE) {
1265 dprintk(1,
1266 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001267 "%s: %s - overlay is already active for another session\n",
1268 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -EBUSY;
1270 }
1271 if (!on && zr->overlay_active != ZORAN_FREE &&
1272 fh->overlay_active == ZORAN_FREE) {
1273 dprintk(1,
1274 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001275 "%s: %s - you cannot cancel someone else's session\n",
1276 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EPERM;
1278 }
1279
1280 if (on == 0) {
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;
1288 } else {
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001289 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 dprintk(1,
1291 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001292 "%s: %s - buffer or window not set\n",
1293 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EINVAL;
1295 }
1296 if (!fh->overlay_settings.format) {
1297 dprintk(1,
1298 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001299 "%s: %s - no overlay format set\n",
1300 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return -EINVAL;
1302 }
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 */
1311 }
1312
1313 /* Make sure the changes come into effect */
1314 return wait_grab_pending(zr);
1315}
1316
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001317/* get the status of a buffer in the clients buffer queue */
1318static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1319 struct v4l2_buffer *buf, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct zoran *zr = fh->zr;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001322 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 buf->flags = V4L2_BUF_FLAG_MAPPED;
1325
1326 switch (fh->map_mode) {
1327 case ZORAN_MAP_MODE_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /* check range */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001329 if (num < 0 || num >= fh->buffers.num_buffers ||
1330 !fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 dprintk(1,
1332 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001333 "%s: %s - wrong number or buffers not allocated\n",
1334 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EINVAL;
1336 }
1337
Trent Piepho1159b7f2009-03-10 23:28:16 -03001338 spin_lock_irqsave(&zr->spinlock, flags);
1339 dprintk(3,
1340 KERN_DEBUG
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);
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001349 buf->length = fh->buffers.buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* get buffer */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001352 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 buf->flags |= V4L2_BUF_FLAG_DONE;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001357 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 } else {
1359 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1360 }
1361
1362 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1363 buf->field = V4L2_FIELD_TOP;
1364 else
1365 buf->field = V4L2_FIELD_INTERLACED;
1366
1367 break;
1368
1369 case ZORAN_MAP_MODE_JPG_REC:
1370 case ZORAN_MAP_MODE_JPG_PLAY:
1371
1372 /* check range */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001373 if (num < 0 || num >= fh->buffers.num_buffers ||
1374 !fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 dprintk(1,
1376 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001377 "%s: %s - wrong number or buffers not allocated\n",
1378 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -EINVAL;
1380 }
1381
1382 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1383 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1384 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001385 buf->length = fh->buffers.buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /* these variables are only written after frame has been captured */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001388 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 buf->flags |= V4L2_BUF_FLAG_DONE;
1394 } else {
1395 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1396 }
1397
1398 /* which fields are these? */
1399 if (fh->jpg_settings.TmpDcm != 1)
Trent Piepho1159b7f2009-03-10 23:28:16 -03001400 buf->field = fh->jpg_settings.odd_even ?
1401 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 else
Trent Piepho1159b7f2009-03-10 23:28:16 -03001403 buf->field = fh->jpg_settings.odd_even ?
1404 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 break;
1407
1408 default:
1409
1410 dprintk(5,
1411 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001412 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1413 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
1415 }
1416
1417 buf->memory = V4L2_MEMORY_MMAP;
1418 buf->index = num;
1419 buf->m.offset = buf->length * num;
1420
1421 return 0;
1422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424static int
1425zoran_set_norm (struct zoran *zr,
Hans Verkuil107063c2009-02-18 17:26:06 -03001426 v4l2_std_id norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Hans Verkuil107063c2009-02-18 17:26:06 -03001428 int on;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (zr->v4l_buffers.active != ZORAN_FREE ||
1431 zr->jpg_buffers.active != ZORAN_FREE) {
1432 dprintk(1,
1433 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -03001434 "%s: %s called while in playback/capture mode\n",
1435 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EBUSY;
1437 }
1438
Hans Verkuil107063c2009-02-18 17:26:06 -03001439 if (!(norm & zr->card.norms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 dprintk(1,
Trent Piephoc61402b2009-03-10 23:28:33 -03001441 KERN_ERR "%s: %s - unsupported norm %llx\n",
1442 ZR_DEVNAME(zr), __func__, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EINVAL;
1444 }
1445
Hans Verkuil107063c2009-02-18 17:26:06 -03001446 if (norm == V4L2_STD_ALL) {
1447 int status = 0;
1448 v4l2_std_id std = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001450 decoder_call(zr, video, querystd, &std);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001451 decoder_call(zr, core, s_std, std);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /* let changes come into effect */
1454 ssleep(2);
1455
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001456 decoder_call(zr, video, g_input_status, &status);
Hans Verkuil107063c2009-02-18 17:26:06 -03001457 if (status & V4L2_IN_ST_NO_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dprintk(1,
1459 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001460 "%s: %s - no norm detected\n",
1461 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* reset norm */
Hans Verkuilf41737e2009-04-01 03:52:39 -03001463 decoder_call(zr, core, s_std, zr->norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EIO;
1465 }
1466
Hans Verkuil107063c2009-02-18 17:26:06 -03001467 norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Hans Verkuil107063c2009-02-18 17:26:06 -03001469 if (norm & V4L2_STD_SECAM)
1470 zr->timing = zr->card.tvn[2];
1471 else if (norm & V4L2_STD_NTSC)
1472 zr->timing = zr->card.tvn[1];
1473 else
1474 zr->timing = zr->card.tvn[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* We switch overlay off and on since a change in the
1477 * norm needs different VFE settings */
1478 on = zr->overlay_active && !zr->v4l_memgrab_active;
1479 if (on)
1480 zr36057_overlay(zr, 0);
1481
Hans Verkuilf41737e2009-04-01 03:52:39 -03001482 decoder_call(zr, core, s_std, norm);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001483 encoder_call(zr, video, s_std_output, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 if (on)
1486 zr36057_overlay(zr, 1);
1487
1488 /* Make sure the changes come into effect */
1489 zr->norm = norm;
1490
1491 return 0;
1492}
1493
1494static int
1495zoran_set_input (struct zoran *zr,
1496 int input)
1497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (input == zr->input) {
1499 return 0;
1500 }
1501
1502 if (zr->v4l_buffers.active != ZORAN_FREE ||
1503 zr->jpg_buffers.active != ZORAN_FREE) {
1504 dprintk(1,
1505 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -03001506 "%s: %s called while in playback/capture mode\n",
1507 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EBUSY;
1509 }
1510
1511 if (input < 0 || input >= zr->card.inputs) {
1512 dprintk(1,
1513 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001514 "%s: %s - unnsupported input %d\n",
1515 ZR_DEVNAME(zr), __func__, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EINVAL;
1517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 zr->input = input;
1520
Hans Verkuil5325b422009-04-02 11:26:22 -03001521 decoder_call(zr, video, s_routing,
1522 zr->card.input[input].muxsel, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 return 0;
1525}
1526
1527/*
1528 * ioctl routine
1529 */
1530
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001531#ifdef CONFIG_VIDEO_V4L1_COMPAT
Hans Verkuil96b8e142009-02-18 13:13:31 -03001532static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Hans Verkuil96b8e142009-02-18 13:13:31 -03001534 struct zoran_fh *fh = __fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct zoran *zr = fh->zr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 struct zoran_jpg_settings settings;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 case BUZIOC_G_PARAMS:
1540 {
1541 struct zoran_params *bparams = arg;
1542
1543 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1544
1545 memset(bparams, 0, sizeof(struct zoran_params));
1546 bparams->major_version = MAJOR_VERSION;
1547 bparams->minor_version = MINOR_VERSION;
1548
Ingo Molnar384c3682006-03-22 03:54:16 -03001549 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Hans Verkuil107063c2009-02-18 17:26:06 -03001551 if (zr->norm & V4L2_STD_NTSC)
1552 bparams->norm = VIDEO_MODE_NTSC;
1553 else if (zr->norm & V4L2_STD_PAL)
1554 bparams->norm = VIDEO_MODE_PAL;
1555 else
1556 bparams->norm = VIDEO_MODE_SECAM;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 bparams->input = zr->input;
1559
1560 bparams->decimation = fh->jpg_settings.decimation;
1561 bparams->HorDcm = fh->jpg_settings.HorDcm;
1562 bparams->VerDcm = fh->jpg_settings.VerDcm;
1563 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1564 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1565 bparams->img_x = fh->jpg_settings.img_x;
1566 bparams->img_y = fh->jpg_settings.img_y;
1567 bparams->img_width = fh->jpg_settings.img_width;
1568 bparams->img_height = fh->jpg_settings.img_height;
1569 bparams->odd_even = fh->jpg_settings.odd_even;
1570
1571 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1572 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1573 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1574 memcpy(bparams->APP_data,
1575 fh->jpg_settings.jpg_comp.APP_data,
1576 sizeof(bparams->APP_data));
1577 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1578 memcpy(bparams->COM_data,
1579 fh->jpg_settings.jpg_comp.COM_data,
1580 sizeof(bparams->COM_data));
1581 bparams->jpeg_markers =
1582 fh->jpg_settings.jpg_comp.jpeg_markers;
1583
Ingo Molnar384c3682006-03-22 03:54:16 -03001584 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 bparams->VFIFO_FB = 0;
1587
1588 return 0;
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 case BUZIOC_S_PARAMS:
1592 {
1593 struct zoran_params *bparams = arg;
1594 int res = 0;
1595
1596 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1597
1598 settings.decimation = bparams->decimation;
1599 settings.HorDcm = bparams->HorDcm;
1600 settings.VerDcm = bparams->VerDcm;
1601 settings.TmpDcm = bparams->TmpDcm;
1602 settings.field_per_buff = bparams->field_per_buff;
1603 settings.img_x = bparams->img_x;
1604 settings.img_y = bparams->img_y;
1605 settings.img_width = bparams->img_width;
1606 settings.img_height = bparams->img_height;
1607 settings.odd_even = bparams->odd_even;
1608
1609 settings.jpg_comp.quality = bparams->quality;
1610 settings.jpg_comp.APPn = bparams->APPn;
1611 settings.jpg_comp.APP_len = bparams->APP_len;
1612 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1613 sizeof(bparams->APP_data));
1614 settings.jpg_comp.COM_len = bparams->COM_len;
1615 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1616 sizeof(bparams->COM_data));
1617 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1618
Ingo Molnar384c3682006-03-22 03:54:16 -03001619 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 if (zr->codec_mode != BUZ_MODE_IDLE) {
1622 dprintk(1,
1623 KERN_ERR
1624 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1625 ZR_DEVNAME(zr));
1626 res = -EINVAL;
1627 goto sparams_unlock_and_return;
1628 }
1629
1630 /* Check the params first before overwriting our
1631 * nternal values */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03001632 if (zoran_check_jpg_settings(zr, &settings, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 res = -EINVAL;
1634 goto sparams_unlock_and_return;
1635 }
1636
1637 fh->jpg_settings = settings;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001638sparams_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001639 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 return res;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 case BUZIOC_REQBUFS:
1645 {
1646 struct zoran_requestbuffers *breq = arg;
1647 int res = 0;
1648
1649 dprintk(3,
1650 KERN_DEBUG
1651 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1652 ZR_DEVNAME(zr), breq->count, breq->size);
1653
1654 /* Enforce reasonable lower and upper limits */
1655 if (breq->count < 4)
1656 breq->count = 4; /* Could be choosen smaller */
1657 if (breq->count > jpg_nbufs)
1658 breq->count = jpg_nbufs;
1659 breq->size = PAGE_ALIGN(breq->size);
1660 if (breq->size < 8192)
1661 breq->size = 8192; /* Arbitrary */
1662 /* breq->size is limited by 1 page for the stat_com
1663 * tables to a Maximum of 2 MB */
1664 if (breq->size > jpg_bufsize)
1665 breq->size = jpg_bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Ingo Molnar384c3682006-03-22 03:54:16 -03001667 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Trent Piepho1159b7f2009-03-10 23:28:16 -03001669 if (fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 dprintk(1,
1671 KERN_ERR
Hans Verkuilb80696b2009-02-18 13:20:05 -03001672 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 ZR_DEVNAME(zr));
1674 res = -EBUSY;
1675 goto jpgreqbuf_unlock_and_return;
1676 }
1677
Trent Piepho1159b7f2009-03-10 23:28:16 -03001678 /* The next mmap will map the MJPEG buffers - could
1679 * also be *_PLAY, but it doesn't matter here */
1680 map_mode_jpg(fh, 0);
1681 fh->buffers.num_buffers = breq->count;
1682 fh->buffers.buffer_size = breq->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001684 if (jpg_fbuffer_alloc(fh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 res = -ENOMEM;
1686 goto jpgreqbuf_unlock_and_return;
1687 }
1688
Hans Verkuil96b8e142009-02-18 13:13:31 -03001689jpgreqbuf_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001690 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 return res;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 case BUZIOC_QBUF_CAPT:
1696 {
1697 int *frame = arg, res;
1698
1699 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1700 ZR_DEVNAME(zr), *frame);
1701
Ingo Molnar384c3682006-03-22 03:54:16 -03001702 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001703 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
Ingo Molnar384c3682006-03-22 03:54:16 -03001704 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 return res;
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 case BUZIOC_QBUF_PLAY:
1710 {
1711 int *frame = arg, res;
1712
1713 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1714 ZR_DEVNAME(zr), *frame);
1715
Ingo Molnar384c3682006-03-22 03:54:16 -03001716 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001717 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
Ingo Molnar384c3682006-03-22 03:54:16 -03001718 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 return res;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 case BUZIOC_SYNC:
1724 {
1725 struct zoran_sync *bsync = arg;
1726 int res;
1727
1728 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1729
Ingo Molnar384c3682006-03-22 03:54:16 -03001730 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03001731
1732 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1733 dprintk(2, KERN_WARNING
1734 "%s: %s - not in jpg capture mode\n",
1735 ZR_DEVNAME(zr), __func__);
1736 res = -EINVAL;
1737 } else {
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001738 res = jpg_sync(fh, bsync);
Trent Piepho1159b7f2009-03-10 23:28:16 -03001739 }
Ingo Molnar384c3682006-03-22 03:54:16 -03001740 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 return res;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 case BUZIOC_G_STATUS:
1746 {
1747 struct zoran_status *bstat = arg;
Hans Verkuil107063c2009-02-18 17:26:06 -03001748 int status = 0, res = 0;
1749 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1752
1753 if (zr->codec_mode != BUZ_MODE_IDLE) {
1754 dprintk(1,
1755 KERN_ERR
1756 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1757 ZR_DEVNAME(zr));
1758 return -EINVAL;
1759 }
1760
Ingo Molnar384c3682006-03-22 03:54:16 -03001761 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (zr->codec_mode != BUZ_MODE_IDLE) {
1764 dprintk(1,
1765 KERN_ERR
1766 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1767 ZR_DEVNAME(zr));
1768 res = -EINVAL;
1769 goto gstat_unlock_and_return;
1770 }
1771
Hans Verkuil5325b422009-04-02 11:26:22 -03001772 decoder_call(zr, video, s_routing,
1773 zr->card.input[bstat->input].muxsel, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* sleep 1 second */
1776 ssleep(1);
1777
1778 /* Get status of video decoder */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001779 decoder_call(zr, video, querystd, &norm);
1780 decoder_call(zr, video, g_input_status, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* restore previous input and norm */
Hans Verkuil5325b422009-04-02 11:26:22 -03001783 decoder_call(zr, video, s_routing,
1784 zr->card.input[zr->input].muxsel, 0, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03001785gstat_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001786 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 if (!res) {
1789 bstat->signal =
Hans Verkuil107063c2009-02-18 17:26:06 -03001790 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1791 if (norm & V4L2_STD_NTSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 bstat->norm = VIDEO_MODE_NTSC;
Hans Verkuil107063c2009-02-18 17:26:06 -03001793 else if (norm & V4L2_STD_SECAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 bstat->norm = VIDEO_MODE_SECAM;
1795 else
1796 bstat->norm = VIDEO_MODE_PAL;
1797
1798 bstat->color =
Hans Verkuil107063c2009-02-18 17:26:06 -03001799 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
1802 return res;
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 default:
Hans Verkuil96b8e142009-02-18 13:13:31 -03001806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03001808}
1809
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001810static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1811{
1812 struct zoran_fh *fh = __fh;
1813 struct zoran *zr = fh->zr;
1814 int i, res = 0;
1815
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001816
1817 mutex_lock(&zr->resource_lock);
1818
Trent Piepho1159b7f2009-03-10 23:28:16 -03001819 if (fh->buffers.allocated) {
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001820 dprintk(1,
1821 KERN_ERR
1822 "%s: VIDIOCGMBUF - buffers already allocated\n",
1823 ZR_DEVNAME(zr));
1824 res = -EINVAL;
1825 goto v4l1reqbuf_unlock_and_return;
1826 }
1827
Trent Piepho1159b7f2009-03-10 23:28:16 -03001828 /* The next mmap will map the V4L buffers */
1829 map_mode_raw(fh);
1830
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001831 if (v4l_fbuffer_alloc(fh)) {
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001832 res = -ENOMEM;
1833 goto v4l1reqbuf_unlock_and_return;
1834 }
1835
Trent Piepho1159b7f2009-03-10 23:28:16 -03001836 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1837 vmbuf->frames = fh->buffers.num_buffers;
1838 for (i = 0; i < vmbuf->frames; i++)
1839 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1840
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001841v4l1reqbuf_unlock_and_return:
1842 mutex_unlock(&zr->resource_lock);
1843
1844 return res;
1845}
1846#endif
1847
Hans Verkuil96b8e142009-02-18 13:13:31 -03001848static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1849{
1850 struct zoran_fh *fh = __fh;
1851 struct zoran *zr = fh->zr;
1852
1853 memset(cap, 0, sizeof(*cap));
1854 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1855 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1856 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1857 pci_name(zr->pci_dev));
Hans Verkuil602dd482009-02-18 17:39:45 -03001858 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
Hans Verkuil96b8e142009-02-18 13:13:31 -03001859 RELEASE_VERSION);
Hans Verkuil602dd482009-02-18 17:39:45 -03001860 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1861 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return 0;
1863}
1864
Hans Verkuil96b8e142009-02-18 13:13:31 -03001865static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Trent Piepho171f48e2009-04-30 21:03:34 -03001867 unsigned int num, i;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001868
Trent Piepho171f48e2009-04-30 21:03:34 -03001869 for (num = i = 0; i < NUM_FORMATS; i++) {
1870 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1871 strncpy(fmt->description, zoran_formats[i].name,
1872 sizeof(fmt->description) - 1);
1873 /* fmt struct pre-zeroed, so adding '\0' not neeed */
1874 fmt->pixelformat = zoran_formats[i].fourcc;
1875 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1876 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1877 return 0;
1878 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03001879 }
Trent Piepho171f48e2009-04-30 21:03:34 -03001880 return -EINVAL;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001881}
1882
1883static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1884 struct v4l2_fmtdesc *f)
1885{
1886 struct zoran_fh *fh = __fh;
1887 struct zoran *zr = fh->zr;
1888
1889 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1890}
1891
1892static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1893 struct v4l2_fmtdesc *f)
1894{
1895 struct zoran_fh *fh = __fh;
1896 struct zoran *zr = fh->zr;
1897
1898 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1899}
1900
1901static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1902 struct v4l2_fmtdesc *f)
1903{
1904 struct zoran_fh *fh = __fh;
1905 struct zoran *zr = fh->zr;
1906
1907 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1908}
1909
1910static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1911 struct v4l2_format *fmt)
1912{
1913 struct zoran_fh *fh = __fh;
1914 struct zoran *zr = fh->zr;
1915
1916 mutex_lock(&zr->resource_lock);
1917
1918 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
Hans Verkuil5ca75b02009-02-18 17:12:34 -03001919 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
Hans Verkuil96b8e142009-02-18 13:13:31 -03001920 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1921 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1922 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1923 if (fh->jpg_settings.TmpDcm == 1)
1924 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
Hans Verkuil3e667932009-02-18 13:24:56 -03001925 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
Hans Verkuil96b8e142009-02-18 13:13:31 -03001926 else
1927 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1928 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1929 fmt->fmt.pix.bytesperline = 0;
1930 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1931
1932 mutex_unlock(&zr->resource_lock);
1933 return 0;
1934}
1935
1936static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1937 struct v4l2_format *fmt)
1938{
1939 struct zoran_fh *fh = __fh;
1940 struct zoran *zr = fh->zr;
1941
1942 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1943 return zoran_g_fmt_vid_out(file, fh, fmt);
1944
1945 mutex_lock(&zr->resource_lock);
1946 fmt->fmt.pix.width = fh->v4l_settings.width;
1947 fmt->fmt.pix.height = fh->v4l_settings.height;
1948 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1949 fh->v4l_settings.height;
1950 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1951 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1952 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1953 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1954 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1955 else
1956 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1957 mutex_unlock(&zr->resource_lock);
1958 return 0;
1959}
1960
1961static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1962 struct v4l2_format *fmt)
1963{
1964 struct zoran_fh *fh = __fh;
1965 struct zoran *zr = fh->zr;
1966
1967 mutex_lock(&zr->resource_lock);
1968
1969 fmt->fmt.win.w.left = fh->overlay_settings.x;
1970 fmt->fmt.win.w.top = fh->overlay_settings.y;
1971 fmt->fmt.win.w.width = fh->overlay_settings.width;
1972 fmt->fmt.win.w.height = fh->overlay_settings.height;
1973 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1974 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1975 else
1976 fmt->fmt.win.field = V4L2_FIELD_TOP;
1977
1978 mutex_unlock(&zr->resource_lock);
1979 return 0;
1980}
1981
1982static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1983 struct v4l2_format *fmt)
1984{
1985 struct zoran_fh *fh = __fh;
1986 struct zoran *zr = fh->zr;
1987
1988 mutex_lock(&zr->resource_lock);
1989
1990 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1991 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1992 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1993 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1994 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1995 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1996 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1997 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1998
1999 mutex_unlock(&zr->resource_lock);
2000 return 0;
2001}
2002
2003static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2004 struct v4l2_format *fmt)
2005{
2006 struct zoran_fh *fh = __fh;
2007 struct zoran *zr = fh->zr;
2008 struct zoran_jpg_settings settings;
2009 int res = 0;
2010
Hans Verkuil96b8e142009-02-18 13:13:31 -03002011 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2012 return -EINVAL;
2013
2014 mutex_lock(&zr->resource_lock);
2015 settings = fh->jpg_settings;
2016
2017 /* we actually need to set 'real' parameters now */
2018 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2019 settings.TmpDcm = 1;
2020 else
2021 settings.TmpDcm = 2;
2022 settings.decimation = 0;
2023 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2024 settings.VerDcm = 2;
2025 else
2026 settings.VerDcm = 1;
2027 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2028 settings.HorDcm = 4;
2029 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2030 settings.HorDcm = 2;
2031 else
2032 settings.HorDcm = 1;
2033 if (settings.TmpDcm == 1)
2034 settings.field_per_buff = 2;
2035 else
2036 settings.field_per_buff = 1;
2037
Hans Verkuil886fe232009-02-18 18:34:55 -03002038 if (settings.HorDcm > 1) {
2039 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2040 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2041 } else {
2042 settings.img_x = 0;
2043 settings.img_width = BUZ_MAX_WIDTH;
2044 }
2045
Hans Verkuil96b8e142009-02-18 13:13:31 -03002046 /* check */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002047 res = zoran_check_jpg_settings(zr, &settings, 1);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002048 if (res)
2049 goto tryfmt_unlock_and_return;
2050
2051 /* tell the user what we actually did */
2052 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2053 fmt->fmt.pix.height = settings.img_height * 2 /
2054 (settings.TmpDcm * settings.VerDcm);
2055 if (settings.TmpDcm == 1)
2056 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2057 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2058 else
2059 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2060 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2061
2062 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
Hans Verkuil886fe232009-02-18 18:34:55 -03002063 fmt->fmt.pix.bytesperline = 0;
2064 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002065tryfmt_unlock_and_return:
2066 mutex_unlock(&zr->resource_lock);
2067 return res;
2068}
2069
2070static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2071 struct v4l2_format *fmt)
2072{
2073 struct zoran_fh *fh = __fh;
2074 struct zoran *zr = fh->zr;
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002075 int bpp;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002076 int i;
2077
Hans Verkuil96b8e142009-02-18 13:13:31 -03002078 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2079 return zoran_try_fmt_vid_out(file, fh, fmt);
2080
2081 mutex_lock(&zr->resource_lock);
2082
2083 for (i = 0; i < NUM_FORMATS; i++)
2084 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2085 break;
2086
2087 if (i == NUM_FORMATS) {
2088 mutex_unlock(&zr->resource_lock);
2089 return -EINVAL;
2090 }
2091
Trent Piepho728f5b92009-05-30 21:45:46 -03002092 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
2093 v4l_bound_align_image(
2094 &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
2095 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002096 mutex_unlock(&zr->resource_lock);
2097
2098 return 0;
2099}
2100
2101static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2102 struct v4l2_format *fmt)
2103{
2104 struct zoran_fh *fh = __fh;
2105 struct zoran *zr = fh->zr;
2106 int res;
2107
2108 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2109 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2110 fmt->fmt.win.w.width,
2111 fmt->fmt.win.w.height,
2112 fmt->fmt.win.clipcount,
2113 fmt->fmt.win.bitmap);
2114 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002115 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2116 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2117 (struct v4l2_clip __user *)fmt->fmt.win.clips,
2118 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002119 mutex_unlock(&zr->resource_lock);
2120 return res;
2121}
2122
2123static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2124 struct v4l2_format *fmt)
2125{
2126 struct zoran_fh *fh = __fh;
2127 struct zoran *zr = fh->zr;
2128 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2129 struct zoran_jpg_settings settings;
2130 int res = 0;
2131
2132 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2133 fmt->fmt.pix.width, fmt->fmt.pix.height,
2134 fmt->fmt.pix.pixelformat,
2135 (char *) &printformat);
2136 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2137 return -EINVAL;
2138
2139 mutex_lock(&zr->resource_lock);
2140
Trent Piepho1159b7f2009-03-10 23:28:16 -03002141 if (fh->buffers.allocated) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002142 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
Trent Piepho1159b7f2009-03-10 23:28:16 -03002143 ZR_DEVNAME(zr));
Hans Verkuil96b8e142009-02-18 13:13:31 -03002144 res = -EBUSY;
2145 goto sfmtjpg_unlock_and_return;
2146 }
2147
Trent Piepho1159b7f2009-03-10 23:28:16 -03002148 settings = fh->jpg_settings;
2149
Hans Verkuil96b8e142009-02-18 13:13:31 -03002150 /* we actually need to set 'real' parameters now */
Hans Verkuil7f37cc92009-02-18 17:00:37 -03002151 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002152 settings.TmpDcm = 1;
2153 else
2154 settings.TmpDcm = 2;
2155 settings.decimation = 0;
2156 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2157 settings.VerDcm = 2;
2158 else
2159 settings.VerDcm = 1;
2160 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2161 settings.HorDcm = 4;
2162 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2163 settings.HorDcm = 2;
2164 else
2165 settings.HorDcm = 1;
2166 if (settings.TmpDcm == 1)
2167 settings.field_per_buff = 2;
2168 else
2169 settings.field_per_buff = 1;
2170
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002171 if (settings.HorDcm > 1) {
2172 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2173 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2174 } else {
2175 settings.img_x = 0;
2176 settings.img_width = BUZ_MAX_WIDTH;
2177 }
2178
Hans Verkuil96b8e142009-02-18 13:13:31 -03002179 /* check */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002180 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002181 if (res)
2182 goto sfmtjpg_unlock_and_return;
2183
2184 /* it's ok, so set them */
2185 fh->jpg_settings = settings;
2186
Trent Piepho1159b7f2009-03-10 23:28:16 -03002187 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2188 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2189
Hans Verkuil96b8e142009-02-18 13:13:31 -03002190 /* tell the user what we actually did */
2191 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2192 fmt->fmt.pix.height = settings.img_height * 2 /
2193 (settings.TmpDcm * settings.VerDcm);
2194 if (settings.TmpDcm == 1)
2195 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2196 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2197 else
2198 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2199 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002200 fmt->fmt.pix.bytesperline = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002201 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002202 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2203
Hans Verkuil96b8e142009-02-18 13:13:31 -03002204sfmtjpg_unlock_and_return:
2205 mutex_unlock(&zr->resource_lock);
2206 return res;
2207}
2208
2209static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2210 struct v4l2_format *fmt)
2211{
2212 struct zoran_fh *fh = __fh;
2213 struct zoran *zr = fh->zr;
2214 int i;
2215 int res = 0;
2216
2217 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2218 return zoran_s_fmt_vid_out(file, fh, fmt);
2219
2220 for (i = 0; i < NUM_FORMATS; i++)
2221 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2222 break;
2223 if (i == NUM_FORMATS) {
2224 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2225 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2226 return -EINVAL;
2227 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03002228
Hans Verkuil96b8e142009-02-18 13:13:31 -03002229 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002230
2231 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2232 fh->buffers.active != ZORAN_FREE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002233 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2234 ZR_DEVNAME(zr));
2235 res = -EBUSY;
2236 goto sfmtv4l_unlock_and_return;
2237 }
2238 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2239 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2240 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2241 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2242
Trent Piepho1159b7f2009-03-10 23:28:16 -03002243 map_mode_raw(fh);
2244
2245 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2246 &zoran_formats[i]);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002247 if (res)
2248 goto sfmtv4l_unlock_and_return;
2249
Trent Piepho1159b7f2009-03-10 23:28:16 -03002250 /* tell the user the results/missing stuff */
Hans Verkuil96b8e142009-02-18 13:13:31 -03002251 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2252 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2253 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2254 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2255 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2256 else
2257 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2258
Hans Verkuil96b8e142009-02-18 13:13:31 -03002259sfmtv4l_unlock_and_return:
2260 mutex_unlock(&zr->resource_lock);
2261 return res;
2262}
2263
2264static int zoran_g_fbuf(struct file *file, void *__fh,
2265 struct v4l2_framebuffer *fb)
2266{
2267 struct zoran_fh *fh = __fh;
2268 struct zoran *zr = fh->zr;
2269
2270 memset(fb, 0, sizeof(*fb));
2271 mutex_lock(&zr->resource_lock);
Hans Verkuil7f6adea2009-02-19 17:31:17 -03002272 fb->base = zr->vbuf_base;
2273 fb->fmt.width = zr->vbuf_width;
2274 fb->fmt.height = zr->vbuf_height;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002275 if (zr->overlay_settings.format)
2276 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03002277 fb->fmt.bytesperline = zr->vbuf_bytesperline;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002278 mutex_unlock(&zr->resource_lock);
2279 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2280 fb->fmt.field = V4L2_FIELD_INTERLACED;
2281 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2282 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2283
2284 return 0;
2285}
2286
2287static int zoran_s_fbuf(struct file *file, void *__fh,
2288 struct v4l2_framebuffer *fb)
2289{
2290 struct zoran_fh *fh = __fh;
2291 struct zoran *zr = fh->zr;
2292 int i, res = 0;
2293 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2294
2295 for (i = 0; i < NUM_FORMATS; i++)
2296 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2297 break;
2298 if (i == NUM_FORMATS) {
2299 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2300 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2301 (char *)&printformat);
2302 return -EINVAL;
2303 }
2304
2305 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002306 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2307 fb->fmt.height, fb->fmt.bytesperline);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002308 mutex_unlock(&zr->resource_lock);
2309
2310 return res;
2311}
2312
2313static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2314{
2315 struct zoran_fh *fh = __fh;
2316 struct zoran *zr = fh->zr;
2317 int res;
2318
2319 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002320 res = setup_overlay(fh, on);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002321 mutex_unlock(&zr->resource_lock);
2322
2323 return res;
2324}
2325
Hans Verkuil84c1b092009-02-18 17:15:46 -03002326static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2327
Hans Verkuil96b8e142009-02-18 13:13:31 -03002328static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2329{
2330 struct zoran_fh *fh = __fh;
2331 struct zoran *zr = fh->zr;
2332 int res = 0;
2333
2334 if (req->memory != V4L2_MEMORY_MMAP) {
Hans Verkuil84c1b092009-02-18 17:15:46 -03002335 dprintk(2,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002336 KERN_ERR
2337 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2338 ZR_DEVNAME(zr), req->memory);
2339 return -EINVAL;
2340 }
2341
Hans Verkuil84c1b092009-02-18 17:15:46 -03002342 if (req->count == 0)
2343 return zoran_streamoff(file, fh, req->type);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002344
Hans Verkuil84c1b092009-02-18 17:15:46 -03002345 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002346 if (fh->buffers.allocated) {
Hans Verkuil84c1b092009-02-18 17:15:46 -03002347 dprintk(2,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002348 KERN_ERR
Hans Verkuil7f37cc92009-02-18 17:00:37 -03002349 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
Hans Verkuil96b8e142009-02-18 13:13:31 -03002350 ZR_DEVNAME(zr));
2351 res = -EBUSY;
2352 goto v4l2reqbuf_unlock_and_return;
2353 }
2354
2355 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
Trent Piepho1159b7f2009-03-10 23:28:16 -03002356 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002357 /* control user input */
2358 if (req->count < 2)
2359 req->count = 2;
2360 if (req->count > v4l_nbufs)
2361 req->count = v4l_nbufs;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002362
2363 /* The next mmap will map the V4L buffers */
2364 map_mode_raw(fh);
2365 fh->buffers.num_buffers = req->count;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002366
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002367 if (v4l_fbuffer_alloc(fh)) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002368 res = -ENOMEM;
2369 goto v4l2reqbuf_unlock_and_return;
2370 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03002371 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002372 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002373 /* we need to calculate size ourselves now */
2374 if (req->count < 4)
2375 req->count = 4;
2376 if (req->count > jpg_nbufs)
2377 req->count = jpg_nbufs;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002378
2379 /* The next mmap will map the MJPEG buffers */
2380 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2381 fh->buffers.num_buffers = req->count;
2382 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002383
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002384 if (jpg_fbuffer_alloc(fh)) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002385 res = -ENOMEM;
2386 goto v4l2reqbuf_unlock_and_return;
2387 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03002388 } else {
2389 dprintk(1,
2390 KERN_ERR
2391 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2392 ZR_DEVNAME(zr), req->type);
2393 res = -EINVAL;
2394 goto v4l2reqbuf_unlock_and_return;
2395 }
2396v4l2reqbuf_unlock_and_return:
2397 mutex_unlock(&zr->resource_lock);
2398
2399 return res;
2400}
2401
2402static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2403{
2404 struct zoran_fh *fh = __fh;
2405 struct zoran *zr = fh->zr;
Trent Piepho9cfb6a32009-03-03 20:44:45 -03002406 int res;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002407
2408 mutex_lock(&zr->resource_lock);
Trent Piephoe276f7b2009-03-05 08:02:05 -03002409 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002410 mutex_unlock(&zr->resource_lock);
2411
2412 return res;
2413}
2414
2415static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2416{
2417 struct zoran_fh *fh = __fh;
2418 struct zoran *zr = fh->zr;
2419 int res = 0, codec_mode, buf_type;
2420
2421 mutex_lock(&zr->resource_lock);
2422
2423 switch (fh->map_mode) {
2424 case ZORAN_MAP_MODE_RAW:
2425 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2426 dprintk(1, KERN_ERR
2427 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2428 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2429 res = -EINVAL;
2430 goto qbuf_unlock_and_return;
2431 }
2432
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002433 res = zoran_v4l_queue_frame(fh, buf->index);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002434 if (res)
2435 goto qbuf_unlock_and_return;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002436 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002437 zr36057_set_memgrab(zr, 1);
2438 break;
2439
2440 case ZORAN_MAP_MODE_JPG_REC:
2441 case ZORAN_MAP_MODE_JPG_PLAY:
2442 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2443 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2444 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2445 } else {
2446 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2447 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2448 }
2449
2450 if (buf->type != buf_type) {
2451 dprintk(1, KERN_ERR
2452 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2453 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2454 res = -EINVAL;
2455 goto qbuf_unlock_and_return;
2456 }
2457
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002458 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002459 if (res != 0)
2460 goto qbuf_unlock_and_return;
2461 if (zr->codec_mode == BUZ_MODE_IDLE &&
Trent Piepho1159b7f2009-03-10 23:28:16 -03002462 fh->buffers.active == ZORAN_LOCKED)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002463 zr36057_enable_jpg(zr, codec_mode);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002464
Hans Verkuil96b8e142009-02-18 13:13:31 -03002465 break;
2466
2467 default:
2468 dprintk(1, KERN_ERR
2469 "%s: VIDIOC_QBUF - unsupported type %d\n",
2470 ZR_DEVNAME(zr), buf->type);
2471 res = -EINVAL;
2472 break;
2473 }
2474qbuf_unlock_and_return:
2475 mutex_unlock(&zr->resource_lock);
2476
2477 return res;
2478}
2479
2480static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2481{
2482 struct zoran_fh *fh = __fh;
2483 struct zoran *zr = fh->zr;
2484 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2485
2486 mutex_lock(&zr->resource_lock);
2487
2488 switch (fh->map_mode) {
2489 case ZORAN_MAP_MODE_RAW:
2490 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2491 dprintk(1, KERN_ERR
2492 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2493 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2494 res = -EINVAL;
2495 goto dqbuf_unlock_and_return;
2496 }
2497
2498 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2499 if (file->f_flags & O_NONBLOCK &&
2500 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2501 res = -EAGAIN;
2502 goto dqbuf_unlock_and_return;
2503 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002504 res = v4l_sync(fh, num);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002505 if (res)
2506 goto dqbuf_unlock_and_return;
2507 zr->v4l_sync_tail++;
Trent Piephoe276f7b2009-03-05 08:02:05 -03002508 res = zoran_v4l2_buffer_status(fh, buf, num);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002509 break;
2510
2511 case ZORAN_MAP_MODE_JPG_REC:
2512 case ZORAN_MAP_MODE_JPG_PLAY:
2513 {
2514 struct zoran_sync bs;
2515
2516 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2517 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2518 else
2519 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2520
2521 if (buf->type != buf_type) {
2522 dprintk(1, KERN_ERR
2523 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2524 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2525 res = -EINVAL;
2526 goto dqbuf_unlock_and_return;
2527 }
2528
2529 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2530
2531 if (file->f_flags & O_NONBLOCK &&
2532 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2533 res = -EAGAIN;
2534 goto dqbuf_unlock_and_return;
2535 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002536 res = jpg_sync(fh, &bs);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002537 if (res)
2538 goto dqbuf_unlock_and_return;
Trent Piephoe276f7b2009-03-05 08:02:05 -03002539 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002540 break;
2541 }
2542
2543 default:
2544 dprintk(1, KERN_ERR
2545 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2546 ZR_DEVNAME(zr), buf->type);
2547 res = -EINVAL;
2548 break;
2549 }
2550dqbuf_unlock_and_return:
2551 mutex_unlock(&zr->resource_lock);
2552
2553 return res;
2554}
2555
2556static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2557{
2558 struct zoran_fh *fh = __fh;
2559 struct zoran *zr = fh->zr;
2560 int res = 0;
2561
2562 mutex_lock(&zr->resource_lock);
2563
2564 switch (fh->map_mode) {
2565 case ZORAN_MAP_MODE_RAW: /* raw capture */
2566 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002567 fh->buffers.active != ZORAN_ACTIVE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002568 res = -EBUSY;
2569 goto strmon_unlock_and_return;
2570 }
2571
Trent Piepho1159b7f2009-03-10 23:28:16 -03002572 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002573 zr->v4l_settings = fh->v4l_settings;
2574
2575 zr->v4l_sync_tail = zr->v4l_pend_tail;
2576 if (!zr->v4l_memgrab_active &&
2577 zr->v4l_pend_head != zr->v4l_pend_tail) {
2578 zr36057_set_memgrab(zr, 1);
2579 }
2580 break;
2581
2582 case ZORAN_MAP_MODE_JPG_REC:
2583 case ZORAN_MAP_MODE_JPG_PLAY:
2584 /* what is the codec mode right now? */
2585 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002586 fh->buffers.active != ZORAN_ACTIVE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002587 res = -EBUSY;
2588 goto strmon_unlock_and_return;
2589 }
2590
Trent Piepho1159b7f2009-03-10 23:28:16 -03002591 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002592
2593 if (zr->jpg_que_head != zr->jpg_que_tail) {
2594 /* Start the jpeg codec when the first frame is queued */
2595 jpeg_start(zr);
2596 }
2597 break;
2598
2599 default:
2600 dprintk(1,
2601 KERN_ERR
2602 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2603 ZR_DEVNAME(zr), fh->map_mode);
2604 res = -EINVAL;
2605 break;
2606 }
2607strmon_unlock_and_return:
2608 mutex_unlock(&zr->resource_lock);
2609
2610 return res;
2611}
2612
2613static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2614{
2615 struct zoran_fh *fh = __fh;
2616 struct zoran *zr = fh->zr;
2617 int i, res = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002618 unsigned long flags;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002619
2620 mutex_lock(&zr->resource_lock);
2621
2622 switch (fh->map_mode) {
2623 case ZORAN_MAP_MODE_RAW: /* raw capture */
Trent Piepho1159b7f2009-03-10 23:28:16 -03002624 if (fh->buffers.active == ZORAN_FREE &&
Hans Verkuil96b8e142009-02-18 13:13:31 -03002625 zr->v4l_buffers.active != ZORAN_FREE) {
2626 res = -EPERM; /* stay off other's settings! */
2627 goto strmoff_unlock_and_return;
2628 }
2629 if (zr->v4l_buffers.active == ZORAN_FREE)
2630 goto strmoff_unlock_and_return;
2631
Trent Piepho1159b7f2009-03-10 23:28:16 -03002632 spin_lock_irqsave(&zr->spinlock, flags);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002633 /* unload capture */
2634 if (zr->v4l_memgrab_active) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002635
Hans Verkuil96b8e142009-02-18 13:13:31 -03002636 zr36057_set_memgrab(zr, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002637 }
2638
Trent Piepho1159b7f2009-03-10 23:28:16 -03002639 for (i = 0; i < fh->buffers.num_buffers; i++)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002640 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002641 fh->buffers = zr->v4l_buffers;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002642
Trent Piepho1159b7f2009-03-10 23:28:16 -03002643 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002644
2645 zr->v4l_grab_seq = 0;
2646 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2647 zr->v4l_sync_tail = 0;
2648
Trent Piepho1159b7f2009-03-10 23:28:16 -03002649 spin_unlock_irqrestore(&zr->spinlock, flags);
2650
Hans Verkuil96b8e142009-02-18 13:13:31 -03002651 break;
2652
2653 case ZORAN_MAP_MODE_JPG_REC:
2654 case ZORAN_MAP_MODE_JPG_PLAY:
Trent Piepho1159b7f2009-03-10 23:28:16 -03002655 if (fh->buffers.active == ZORAN_FREE &&
Hans Verkuil96b8e142009-02-18 13:13:31 -03002656 zr->jpg_buffers.active != ZORAN_FREE) {
2657 res = -EPERM; /* stay off other's settings! */
2658 goto strmoff_unlock_and_return;
2659 }
2660 if (zr->jpg_buffers.active == ZORAN_FREE)
2661 goto strmoff_unlock_and_return;
2662
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002663 res = jpg_qbuf(fh, -1,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002664 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2665 BUZ_MODE_MOTION_COMPRESS :
2666 BUZ_MODE_MOTION_DECOMPRESS);
2667 if (res)
2668 goto strmoff_unlock_and_return;
2669 break;
2670 default:
2671 dprintk(1, KERN_ERR
2672 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2673 ZR_DEVNAME(zr), fh->map_mode);
2674 res = -EINVAL;
2675 break;
2676 }
2677strmoff_unlock_and_return:
2678 mutex_unlock(&zr->resource_lock);
2679
2680 return res;
2681}
2682
2683static int zoran_queryctrl(struct file *file, void *__fh,
2684 struct v4l2_queryctrl *ctrl)
2685{
Hans Verkuil107063c2009-02-18 17:26:06 -03002686 struct zoran_fh *fh = __fh;
2687 struct zoran *zr = fh->zr;
2688
Hans Verkuil96b8e142009-02-18 13:13:31 -03002689 /* we only support hue/saturation/contrast/brightness */
2690 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2691 ctrl->id > V4L2_CID_HUE)
2692 return -EINVAL;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002693
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002694 decoder_call(zr, core, queryctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002695
2696 return 0;
2697}
2698
2699static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2700{
2701 struct zoran_fh *fh = __fh;
2702 struct zoran *zr = fh->zr;
2703
2704 /* we only support hue/saturation/contrast/brightness */
2705 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2706 ctrl->id > V4L2_CID_HUE)
2707 return -EINVAL;
2708
2709 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002710 decoder_call(zr, core, g_ctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002711 mutex_unlock(&zr->resource_lock);
2712
2713 return 0;
2714}
2715
2716static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2717{
2718 struct zoran_fh *fh = __fh;
2719 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002720
2721 /* we only support hue/saturation/contrast/brightness */
2722 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2723 ctrl->id > V4L2_CID_HUE)
2724 return -EINVAL;
2725
Hans Verkuil96b8e142009-02-18 13:13:31 -03002726 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002727 decoder_call(zr, core, s_ctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002728 mutex_unlock(&zr->resource_lock);
2729
2730 return 0;
2731}
2732
2733static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2734{
2735 struct zoran_fh *fh = __fh;
2736 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002737
2738 mutex_lock(&zr->resource_lock);
Hans Verkuil107063c2009-02-18 17:26:06 -03002739 *std = zr->norm;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002740 mutex_unlock(&zr->resource_lock);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002741 return 0;
2742}
2743
2744static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2745{
2746 struct zoran_fh *fh = __fh;
2747 struct zoran *zr = fh->zr;
Hans Verkuil107063c2009-02-18 17:26:06 -03002748 int res = 0;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002749
2750 mutex_lock(&zr->resource_lock);
Hans Verkuil107063c2009-02-18 17:26:06 -03002751 res = zoran_set_norm(zr, *std);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002752 if (res)
2753 goto sstd_unlock_and_return;
2754
2755 res = wait_grab_pending(zr);
2756sstd_unlock_and_return:
2757 mutex_unlock(&zr->resource_lock);
2758 return res;
2759}
2760
2761static int zoran_enum_input(struct file *file, void *__fh,
2762 struct v4l2_input *inp)
2763{
2764 struct zoran_fh *fh = __fh;
2765 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002766
2767 if (inp->index < 0 || inp->index >= zr->card.inputs)
2768 return -EINVAL;
2769 else {
2770 int id = inp->index;
2771 memset(inp, 0, sizeof(*inp));
2772 inp->index = id;
2773 }
2774
2775 strncpy(inp->name, zr->card.input[inp->index].name,
2776 sizeof(inp->name) - 1);
2777 inp->type = V4L2_INPUT_TYPE_CAMERA;
2778 inp->std = V4L2_STD_ALL;
2779
2780 /* Get status of video decoder */
2781 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002782 decoder_call(zr, video, g_input_status, &inp->status);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002783 mutex_unlock(&zr->resource_lock);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002784 return 0;
2785}
2786
2787static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2788{
2789 struct zoran_fh *fh = __fh;
2790 struct zoran *zr = fh->zr;
2791
2792 mutex_lock(&zr->resource_lock);
2793 *input = zr->input;
2794 mutex_unlock(&zr->resource_lock);
2795
2796 return 0;
2797}
2798
2799static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2800{
2801 struct zoran_fh *fh = __fh;
2802 struct zoran *zr = fh->zr;
2803 int res;
2804
2805 mutex_lock(&zr->resource_lock);
2806 res = zoran_set_input(zr, input);
2807 if (res)
2808 goto sinput_unlock_and_return;
2809
2810 /* Make sure the changes come into effect */
2811 res = wait_grab_pending(zr);
2812sinput_unlock_and_return:
2813 mutex_unlock(&zr->resource_lock);
2814 return res;
2815}
2816
2817static int zoran_enum_output(struct file *file, void *__fh,
2818 struct v4l2_output *outp)
2819{
2820 if (outp->index != 0)
2821 return -EINVAL;
2822
2823 memset(outp, 0, sizeof(*outp));
2824 outp->index = 0;
2825 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2826 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2827
2828 return 0;
2829}
2830
2831static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2832{
2833 *output = 0;
2834
2835 return 0;
2836}
2837
2838static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2839{
2840 if (output != 0)
2841 return -EINVAL;
2842
2843 return 0;
2844}
2845
2846/* cropping (sub-frame capture) */
2847static int zoran_cropcap(struct file *file, void *__fh,
2848 struct v4l2_cropcap *cropcap)
2849{
2850 struct zoran_fh *fh = __fh;
2851 struct zoran *zr = fh->zr;
2852 int type = cropcap->type, res = 0;
2853
2854 memset(cropcap, 0, sizeof(*cropcap));
2855 cropcap->type = type;
2856
2857 mutex_lock(&zr->resource_lock);
2858
2859 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2860 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2861 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2862 dprintk(1, KERN_ERR
2863 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2864 ZR_DEVNAME(zr));
2865 res = -EINVAL;
2866 goto cropcap_unlock_and_return;
2867 }
2868
2869 cropcap->bounds.top = cropcap->bounds.left = 0;
2870 cropcap->bounds.width = BUZ_MAX_WIDTH;
2871 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2872 cropcap->defrect.top = cropcap->defrect.left = 0;
2873 cropcap->defrect.width = BUZ_MIN_WIDTH;
2874 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2875cropcap_unlock_and_return:
2876 mutex_unlock(&zr->resource_lock);
2877 return res;
2878}
2879
2880static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2881{
2882 struct zoran_fh *fh = __fh;
2883 struct zoran *zr = fh->zr;
2884 int type = crop->type, res = 0;
2885
2886 memset(crop, 0, sizeof(*crop));
2887 crop->type = type;
2888
2889 mutex_lock(&zr->resource_lock);
2890
2891 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2892 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2893 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2894 dprintk(1,
2895 KERN_ERR
2896 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2897 ZR_DEVNAME(zr));
2898 res = -EINVAL;
2899 goto gcrop_unlock_and_return;
2900 }
2901
2902 crop->c.top = fh->jpg_settings.img_y;
2903 crop->c.left = fh->jpg_settings.img_x;
2904 crop->c.width = fh->jpg_settings.img_width;
2905 crop->c.height = fh->jpg_settings.img_height;
2906
2907gcrop_unlock_and_return:
2908 mutex_unlock(&zr->resource_lock);
2909
2910 return res;
2911}
2912
2913static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2914{
2915 struct zoran_fh *fh = __fh;
2916 struct zoran *zr = fh->zr;
2917 int res = 0;
2918 struct zoran_jpg_settings settings;
2919
2920 settings = fh->jpg_settings;
2921
2922 mutex_lock(&zr->resource_lock);
2923
Trent Piepho1159b7f2009-03-10 23:28:16 -03002924 if (fh->buffers.allocated) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002925 dprintk(1, KERN_ERR
2926 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2927 ZR_DEVNAME(zr));
2928 res = -EBUSY;
2929 goto scrop_unlock_and_return;
2930 }
2931
2932 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2933 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2934 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2935 dprintk(1, KERN_ERR
2936 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2937 ZR_DEVNAME(zr));
2938 res = -EINVAL;
2939 goto scrop_unlock_and_return;
2940 }
2941
2942 /* move into a form that we understand */
2943 settings.img_x = crop->c.left;
2944 settings.img_y = crop->c.top;
2945 settings.img_width = crop->c.width;
2946 settings.img_height = crop->c.height;
2947
2948 /* check validity */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002949 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002950 if (res)
2951 goto scrop_unlock_and_return;
2952
2953 /* accept */
2954 fh->jpg_settings = settings;
2955
2956scrop_unlock_and_return:
2957 mutex_unlock(&zr->resource_lock);
2958 return res;
2959}
2960
2961static int zoran_g_jpegcomp(struct file *file, void *__fh,
2962 struct v4l2_jpegcompression *params)
2963{
2964 struct zoran_fh *fh = __fh;
2965 struct zoran *zr = fh->zr;
2966 memset(params, 0, sizeof(*params));
2967
2968 mutex_lock(&zr->resource_lock);
2969
2970 params->quality = fh->jpg_settings.jpg_comp.quality;
2971 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2972 memcpy(params->APP_data,
2973 fh->jpg_settings.jpg_comp.APP_data,
2974 fh->jpg_settings.jpg_comp.APP_len);
2975 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2976 memcpy(params->COM_data,
2977 fh->jpg_settings.jpg_comp.COM_data,
2978 fh->jpg_settings.jpg_comp.COM_len);
2979 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2980 params->jpeg_markers =
2981 fh->jpg_settings.jpg_comp.jpeg_markers;
2982
2983 mutex_unlock(&zr->resource_lock);
2984
2985 return 0;
2986}
2987
2988static int zoran_s_jpegcomp(struct file *file, void *__fh,
2989 struct v4l2_jpegcompression *params)
2990{
2991 struct zoran_fh *fh = __fh;
2992 struct zoran *zr = fh->zr;
2993 int res = 0;
2994 struct zoran_jpg_settings settings;
2995
2996 settings = fh->jpg_settings;
2997
2998 settings.jpg_comp = *params;
2999
3000 mutex_lock(&zr->resource_lock);
3001
Trent Piepho1159b7f2009-03-10 23:28:16 -03003002 if (fh->buffers.active != ZORAN_FREE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03003003 dprintk(1, KERN_WARNING
3004 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3005 ZR_DEVNAME(zr));
3006 res = -EBUSY;
3007 goto sjpegc_unlock_and_return;
3008 }
3009
Hans Verkuil0ba514d2009-02-18 17:11:17 -03003010 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03003011 if (res)
3012 goto sjpegc_unlock_and_return;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003013 if (!fh->buffers.allocated)
3014 fh->buffers.buffer_size =
3015 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
Hans Verkuil96b8e142009-02-18 13:13:31 -03003016 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3017sjpegc_unlock_and_return:
3018 mutex_unlock(&zr->resource_lock);
3019
Hans Verkuil27f79522009-02-20 07:34:28 -03003020 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023static unsigned int
3024zoran_poll (struct file *file,
3025 poll_table *wait)
3026{
3027 struct zoran_fh *fh = file->private_data;
3028 struct zoran *zr = fh->zr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 int res = 0, frame;
Trent Piephoe42af832007-07-17 18:29:43 -03003030 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 /* we should check whether buffers are ready to be synced on
3033 * (w/o waits - O_NONBLOCK) here
3034 * if ready for read (sync), return POLLIN|POLLRDNORM,
3035 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3036 * if error, return POLLERR,
3037 * if no buffers queued or so, return POLLNVAL
3038 */
3039
Ingo Molnar384c3682006-03-22 03:54:16 -03003040 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 switch (fh->map_mode) {
3043 case ZORAN_MAP_MODE_RAW:
Trent Piephoe42af832007-07-17 18:29:43 -03003044 poll_wait(file, &zr->v4l_capq, wait);
3045 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3046
3047 spin_lock_irqsave(&zr->spinlock, flags);
3048 dprintk(3,
3049 KERN_DEBUG
3050 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -03003051 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003052 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
Trent Piephoe42af832007-07-17 18:29:43 -03003053 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3054 zr->v4l_pend_tail, zr->v4l_pend_head);
3055 /* Process is the one capturing? */
Trent Piepho1159b7f2009-03-10 23:28:16 -03003056 if (fh->buffers.active != ZORAN_FREE &&
Trent Piephoe42af832007-07-17 18:29:43 -03003057 /* Buffer ready to DQBUF? */
3058 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 res = POLLIN | POLLRDNORM;
Trent Piephoe42af832007-07-17 18:29:43 -03003060 spin_unlock_irqrestore(&zr->spinlock, flags);
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 break;
3063
3064 case ZORAN_MAP_MODE_JPG_REC:
3065 case ZORAN_MAP_MODE_JPG_PLAY:
Trent Piephoe42af832007-07-17 18:29:43 -03003066 poll_wait(file, &zr->jpg_capq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
Trent Piephoe42af832007-07-17 18:29:43 -03003068
3069 spin_lock_irqsave(&zr->spinlock, flags);
3070 dprintk(3,
3071 KERN_DEBUG
3072 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -03003073 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003074 "FAL"[fh->buffers.active], zr->jpg_que_tail,
Trent Piephoe42af832007-07-17 18:29:43 -03003075 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3076 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003077 if (fh->buffers.active != ZORAN_FREE &&
Trent Piephoe42af832007-07-17 18:29:43 -03003078 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3080 res = POLLIN | POLLRDNORM;
3081 else
3082 res = POLLOUT | POLLWRNORM;
3083 }
Trent Piephoe42af832007-07-17 18:29:43 -03003084 spin_unlock_irqrestore(&zr->spinlock, flags);
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 break;
3087
3088 default:
3089 dprintk(1,
Trent Piephoe42af832007-07-17 18:29:43 -03003090 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003091 "%s: %s - internal error, unknown map_mode=%d\n",
3092 ZR_DEVNAME(zr), __func__, fh->map_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 res = POLLNVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095
Ingo Molnar384c3682006-03-22 03:54:16 -03003096 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 return res;
3099}
3100
3101
3102/*
3103 * This maps the buffers to user space.
3104 *
3105 * Depending on the state of fh->map_mode
3106 * the V4L or the MJPEG buffers are mapped
3107 * per buffer or all together
3108 *
3109 * Note that we need to connect to some
3110 * unmap signal event to unmap the de-allocate
3111 * the buffer accordingly (zoran_vm_close())
3112 */
3113
3114static void
3115zoran_vm_open (struct vm_area_struct *vma)
3116{
3117 struct zoran_mapping *map = vma->vm_private_data;
3118
3119 map->count++;
3120}
3121
3122static void
3123zoran_vm_close (struct vm_area_struct *vma)
3124{
3125 struct zoran_mapping *map = vma->vm_private_data;
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003126 struct zoran_fh *fh = map->file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 struct zoran *zr = fh->zr;
3128 int i;
3129
Trent Piepho1159b7f2009-03-10 23:28:16 -03003130 if (--map->count > 0)
3131 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Trent Piepho1159b7f2009-03-10 23:28:16 -03003133 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3134 __func__, mode_name(fh->map_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Trent Piepho1159b7f2009-03-10 23:28:16 -03003136 for (i = 0; i < fh->buffers.num_buffers; i++) {
3137 if (fh->buffers.buffer[i].map == map)
3138 fh->buffers.buffer[i].map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003140 kfree(map);
3141
3142 /* Any buffers still mapped? */
3143 for (i = 0; i < fh->buffers.num_buffers; i++)
3144 if (fh->buffers.buffer[i].map)
3145 return;
3146
3147 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3148 __func__, mode_name(fh->map_mode));
3149
3150 mutex_lock(&zr->resource_lock);
3151
3152 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3153 if (fh->buffers.active != ZORAN_FREE) {
3154 unsigned long flags;
3155
3156 spin_lock_irqsave(&zr->spinlock, flags);
3157 zr36057_set_memgrab(zr, 0);
3158 zr->v4l_buffers.allocated = 0;
3159 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3160 spin_unlock_irqrestore(&zr->spinlock, flags);
3161 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003162 v4l_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003163 } else {
3164 if (fh->buffers.active != ZORAN_FREE) {
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003165 jpg_qbuf(fh, -1, zr->codec_mode);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003166 zr->jpg_buffers.allocated = 0;
3167 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3168 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003169 jpg_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003170 }
3171
3172 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
3175static struct vm_operations_struct zoran_vm_ops = {
3176 .open = zoran_vm_open,
3177 .close = zoran_vm_close,
3178};
3179
3180static int
3181zoran_mmap (struct file *file,
3182 struct vm_area_struct *vma)
3183{
3184 struct zoran_fh *fh = file->private_data;
3185 struct zoran *zr = fh->zr;
3186 unsigned long size = (vma->vm_end - vma->vm_start);
3187 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3188 int i, j;
3189 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3190 int first, last;
3191 struct zoran_mapping *map;
3192 int res = 0;
3193
3194 dprintk(3,
Trent Piephoc61402b2009-03-10 23:28:33 -03003195 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3196 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003197 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3200 !(vma->vm_flags & VM_WRITE)) {
3201 dprintk(1,
3202 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003203 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3204 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return -EINVAL;
3206 }
3207
Trent Piepho1159b7f2009-03-10 23:28:16 -03003208 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Trent Piepho1159b7f2009-03-10 23:28:16 -03003210 if (!fh->buffers.allocated) {
3211 dprintk(1,
3212 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003213 "%s: %s(%s) - buffers not yet allocated\n",
3214 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
Trent Piepho1159b7f2009-03-10 23:28:16 -03003215 res = -ENOMEM;
3216 goto mmap_unlock_and_return;
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Trent Piepho1159b7f2009-03-10 23:28:16 -03003219 first = offset / fh->buffers.buffer_size;
3220 last = first - 1 + size / fh->buffers.buffer_size;
3221 if (offset % fh->buffers.buffer_size != 0 ||
3222 size % fh->buffers.buffer_size != 0 || first < 0 ||
3223 last < 0 || first >= fh->buffers.num_buffers ||
3224 last >= fh->buffers.buffer_size) {
3225 dprintk(1,
3226 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003227 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3228 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003229 fh->buffers.buffer_size,
3230 fh->buffers.num_buffers);
3231 res = -EINVAL;
3232 goto mmap_unlock_and_return;
3233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Trent Piepho1159b7f2009-03-10 23:28:16 -03003235 /* Check if any buffers are already mapped */
3236 for (i = first; i <= last; i++) {
3237 if (fh->buffers.buffer[i].map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 dprintk(1,
3239 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003240 "%s: %s(%s) - buffer %d already mapped\n",
3241 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003242 res = -EBUSY;
3243 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trent Piepho1159b7f2009-03-10 23:28:16 -03003247 /* map these buffers */
3248 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3249 if (!map) {
3250 res = -ENOMEM;
3251 goto mmap_unlock_and_return;
3252 }
3253 map->file = file;
3254 map->count = 1;
3255
3256 vma->vm_ops = &zoran_vm_ops;
3257 vma->vm_flags |= VM_DONTEXPAND;
3258 vma->vm_private_data = map;
3259
3260 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 for (i = first; i <= last; i++) {
Trent Piepho1159b7f2009-03-10 23:28:16 -03003262 todo = size;
3263 if (todo > fh->buffers.buffer_size)
3264 todo = fh->buffers.buffer_size;
3265 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3266 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3267 todo, PAGE_SHARED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 dprintk(1,
3269 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003270 "%s: %s(V4L) - remap_pfn_range failed\n",
3271 ZR_DEVNAME(zr), __func__);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003272 res = -EAGAIN;
3273 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003275 size -= todo;
3276 start += todo;
3277 fh->buffers.buffer[i].map = map;
3278 if (size == 0)
3279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003281 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 for (i = first; i <= last; i++) {
3283 for (j = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003284 j < fh->buffers.buffer_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 j++) {
3286 fraglen =
Trent Piepho1159b7f2009-03-10 23:28:16 -03003287 (le32_to_cpu(fh->buffers.buffer[i].jpg.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 frag_tab[2 * j + 1]) & ~1) << 1;
3289 todo = size;
3290 if (todo > fraglen)
3291 todo = fraglen;
3292 pos =
Trent Piepho1159b7f2009-03-10 23:28:16 -03003293 le32_to_cpu(fh->buffers.
3294 buffer[i].jpg.frag_tab[2 * j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /* should just be pos on i386 */
3296 page = virt_to_phys(bus_to_virt(pos))
3297 >> PAGE_SHIFT;
3298 if (remap_pfn_range(vma, start, page,
3299 todo, PAGE_SHARED)) {
3300 dprintk(1,
3301 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003302 "%s: %s(V4L) - remap_pfn_range failed\n",
3303 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 res = -EAGAIN;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003305 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 size -= todo;
3308 start += todo;
3309 if (size == 0)
3310 break;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003311 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 frag_tab[2 * j + 1]) & 1)
3313 break; /* was last fragment */
3314 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003315 fh->buffers.buffer[i].map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (size == 0)
3317 break;
3318
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
Trent Piepho1159b7f2009-03-10 23:28:16 -03003322mmap_unlock_and_return:
3323 mutex_unlock(&zr->resource_lock);
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return 0;
3326}
3327
Hans Verkuil96b8e142009-02-18 13:13:31 -03003328static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3329 .vidioc_querycap = zoran_querycap,
3330 .vidioc_cropcap = zoran_cropcap,
3331 .vidioc_s_crop = zoran_s_crop,
3332 .vidioc_g_crop = zoran_g_crop,
3333 .vidioc_enum_input = zoran_enum_input,
3334 .vidioc_g_input = zoran_g_input,
3335 .vidioc_s_input = zoran_s_input,
3336 .vidioc_enum_output = zoran_enum_output,
3337 .vidioc_g_output = zoran_g_output,
3338 .vidioc_s_output = zoran_s_output,
3339 .vidioc_g_fbuf = zoran_g_fbuf,
3340 .vidioc_s_fbuf = zoran_s_fbuf,
3341 .vidioc_g_std = zoran_g_std,
3342 .vidioc_s_std = zoran_s_std,
3343 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3344 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3345 .vidioc_overlay = zoran_overlay,
3346 .vidioc_reqbufs = zoran_reqbufs,
3347 .vidioc_querybuf = zoran_querybuf,
3348 .vidioc_qbuf = zoran_qbuf,
3349 .vidioc_dqbuf = zoran_dqbuf,
3350 .vidioc_streamon = zoran_streamon,
3351 .vidioc_streamoff = zoran_streamoff,
3352 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3353 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3354 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3355 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3356 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3357 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3358 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3359 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3360 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3361 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3362 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3363 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3364 .vidioc_queryctrl = zoran_queryctrl,
3365 .vidioc_s_ctrl = zoran_s_ctrl,
3366 .vidioc_g_ctrl = zoran_g_ctrl,
Hans Verkuildcbd83b2009-02-18 13:51:13 -03003367#ifdef CONFIG_VIDEO_V4L1_COMPAT
Hans Verkuil96b8e142009-02-18 13:13:31 -03003368 .vidioc_default = zoran_default,
Hans Verkuildcbd83b2009-02-18 13:51:13 -03003369 .vidiocgmbuf = zoran_vidiocgmbuf,
3370#endif
Hans Verkuil96b8e142009-02-18 13:13:31 -03003371};
3372
Hans Verkuilbec43662008-12-30 06:58:20 -03003373static const struct v4l2_file_operations zoran_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 .owner = THIS_MODULE,
3375 .open = zoran_open,
3376 .release = zoran_close,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003377 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 .read = zoran_read,
3379 .write = zoran_write,
3380 .mmap = zoran_mmap,
3381 .poll = zoran_poll,
3382};
3383
3384struct video_device zoran_template __devinitdata = {
3385 .name = ZORAN_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 .fops = &zoran_fops,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003387 .ioctl_ops = &zoran_ioctl_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 .release = &zoran_vdev_release,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003389 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 .minor = -1
3391};
3392