blob: 3d7df32a3d8706d30ac56a88fcbc720e7a25a6d6 [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>
52#include <linux/pci.h>
53#include <linux/vmalloc.h>
54#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <linux/interrupt.h>
57#include <linux/i2c.h>
58#include <linux/i2c-algo-bit.h>
59
60#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Mauro Carvalho Chehab7e0a16f2009-03-10 05:31:34 -030062#include <linux/videodev.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030063#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030064#include <media/v4l2-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "videocodec.h"
66
Harvey Harrison9a6ab762008-05-16 11:20:25 -070067#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/io.h>
69#include <asm/uaccess.h>
70#include <linux/proc_fs.h>
71
Ingo Molnar384c3682006-03-22 03:54:16 -030072#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include "zoran.h"
74#include "zoran_device.h"
75#include "zoran_card.h"
76
Trent Piepho603d6f22007-07-17 18:29:44 -030077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078const struct zoran_format zoran_formats[] = {
79 {
Trent Piepho603d6f22007-07-17 18:29:44 -030080 .name = "15-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -030081 .fourcc = V4L2_PIX_FMT_RGB555,
82 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .depth = 15,
84 .flags = ZORAN_FORMAT_CAPTURE |
85 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -030086 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
87 ZR36057_VFESPFR_LittleEndian,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }, {
Trent Piepho603d6f22007-07-17 18:29:44 -030089 .name = "15-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -030090 .fourcc = V4L2_PIX_FMT_RGB555X,
91 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -030092 .depth = 15,
93 .flags = ZORAN_FORMAT_CAPTURE |
94 ZORAN_FORMAT_OVERLAY,
95 .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
96 }, {
97 .name = "16-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -030098 .fourcc = V4L2_PIX_FMT_RGB565,
99 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .depth = 16,
101 .flags = ZORAN_FORMAT_CAPTURE |
102 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300103 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
104 ZR36057_VFESPFR_LittleEndian,
105 }, {
106 .name = "16-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300107 .fourcc = V4L2_PIX_FMT_RGB565X,
108 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -0300109 .depth = 16,
110 .flags = ZORAN_FORMAT_CAPTURE |
111 ZORAN_FORMAT_OVERLAY,
112 .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }, {
114 .name = "24-bit RGB",
Hans Verkuil602dd482009-02-18 17:39:45 -0300115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .depth = 24,
118 .flags = ZORAN_FORMAT_CAPTURE |
119 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300120 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }, {
Trent Piepho603d6f22007-07-17 18:29:44 -0300122 .name = "32-bit RGB LE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300123 .fourcc = V4L2_PIX_FMT_BGR32,
124 .colorspace = V4L2_COLORSPACE_SRGB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .depth = 32,
126 .flags = ZORAN_FORMAT_CAPTURE |
127 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300128 .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
129 }, {
130 .name = "32-bit RGB BE",
Hans Verkuil602dd482009-02-18 17:39:45 -0300131 .fourcc = V4L2_PIX_FMT_RGB32,
132 .colorspace = V4L2_COLORSPACE_SRGB,
Trent Piepho603d6f22007-07-17 18:29:44 -0300133 .depth = 32,
134 .flags = ZORAN_FORMAT_CAPTURE |
135 ZORAN_FORMAT_OVERLAY,
136 .vfespfr = ZR36057_VFESPFR_RGB888,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }, {
138 .name = "4:2:2, packed, YUYV",
Hans Verkuil602dd482009-02-18 17:39:45 -0300139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .depth = 16,
142 .flags = ZORAN_FORMAT_CAPTURE |
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300143 ZORAN_FORMAT_OVERLAY,
Trent Piepho603d6f22007-07-17 18:29:44 -0300144 .vfespfr = ZR36057_VFESPFR_YUV422,
145 }, {
146 .name = "4:2:2, packed, UYVY",
Hans Verkuil602dd482009-02-18 17:39:45 -0300147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Trent Piepho603d6f22007-07-17 18:29:44 -0300149 .depth = 16,
150 .flags = ZORAN_FORMAT_CAPTURE |
151 ZORAN_FORMAT_OVERLAY,
152 .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }, {
154 .name = "Hardware-encoded Motion-JPEG",
Hans Verkuil602dd482009-02-18 17:39:45 -0300155 .fourcc = V4L2_PIX_FMT_MJPEG,
156 .colorspace = V4L2_COLORSPACE_SMPTE170M,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .depth = 0,
158 .flags = ZORAN_FORMAT_CAPTURE |
159 ZORAN_FORMAT_PLAYBACK |
160 ZORAN_FORMAT_COMPRESSED,
161 }
162};
Trent Piepho603d6f22007-07-17 18:29:44 -0300163#define NUM_FORMATS ARRAY_SIZE(zoran_formats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* small helper function for calculating buffersizes for v4l2
166 * we calculate the nearest higher power-of-two, which
167 * will be the recommended buffersize */
168static __u32
169zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
170{
171 __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
172 __u32 num = (1024 * 512) / (div);
173 __u32 result = 2;
174
175 num--;
176 while (num) {
177 num >>= 1;
178 result <<= 1;
179 }
180
181 if (result > jpg_bufsize)
182 return jpg_bufsize;
183 if (result < 8192)
184 return 8192;
185 return result;
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* forward references */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300189static void v4l_fbuffer_free(struct zoran_fh *fh);
190static void jpg_fbuffer_free(struct zoran_fh *fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Trent Piepho1159b7f2009-03-10 23:28:16 -0300192/* Set mapping mode */
193static void map_mode_raw(struct zoran_fh *fh)
194{
195 fh->map_mode = ZORAN_MAP_MODE_RAW;
196 fh->buffers.buffer_size = v4l_bufsize;
197 fh->buffers.num_buffers = v4l_nbufs;
198}
199static void map_mode_jpg(struct zoran_fh *fh, int play)
200{
201 fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
202 fh->buffers.buffer_size = jpg_bufsize;
203 fh->buffers.num_buffers = jpg_nbufs;
204}
205static inline const char *mode_name(enum zoran_map_mode mode)
206{
207 return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * Allocate the V4L grab buffers
212 *
213 * These have to be pysically contiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300216static int v4l_fbuffer_alloc(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct zoran *zr = fh->zr;
219 int i, off;
220 unsigned char *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Trent Piepho1159b7f2009-03-10 23:28:16 -0300222 for (i = 0; i < fh->buffers.num_buffers; i++) {
223 if (fh->buffers.buffer[i].v4l.fbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dprintk(2,
225 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300226 "%s: %s - buffer %d already allocated!?\n",
227 ZR_DEVNAME(zr), __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 //udelay(20);
Jean Delvareee9a9d62009-03-10 23:28:20 -0300230 mem = kmalloc(fh->buffers.buffer_size,
231 GFP_KERNEL | __GFP_NOWARN);
Hans Verkuilb80696b2009-02-18 13:20:05 -0300232 if (!mem) {
233 dprintk(1,
234 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300235 "%s: %s - kmalloc for V4L buf %d failed\n",
236 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300237 v4l_fbuffer_free(fh);
Hans Verkuilb80696b2009-02-18 13:20:05 -0300238 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300240 fh->buffers.buffer[i].v4l.fbuffer = mem;
241 fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
242 fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
243 for (off = 0; off < fh->buffers.buffer_size;
Hans Verkuilb80696b2009-02-18 13:20:05 -0300244 off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300245 SetPageReserved(virt_to_page(mem + off));
Hans Verkuilb80696b2009-02-18 13:20:05 -0300246 dprintk(4,
247 KERN_INFO
Randy Dunlap8737f662009-03-27 14:01:11 -0300248 "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
Trent Piephoc61402b2009-03-10 23:28:33 -0300249 ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
Randy Dunlap8737f662009-03-27 14:01:11 -0300250 (unsigned long long)virt_to_bus(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Trent Piepho1159b7f2009-03-10 23:28:16 -0300253 fh->buffers.allocated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return 0;
256}
257
258/* free the V4L grab buffers */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300259static void v4l_fbuffer_free(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct zoran *zr = fh->zr;
262 int i, off;
263 unsigned char *mem;
264
Trent Piephoc61402b2009-03-10 23:28:33 -0300265 dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Trent Piepho1159b7f2009-03-10 23:28:16 -0300267 for (i = 0; i < fh->buffers.num_buffers; i++) {
268 if (!fh->buffers.buffer[i].v4l.fbuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 continue;
270
Trent Piepho1159b7f2009-03-10 23:28:16 -0300271 mem = fh->buffers.buffer[i].v4l.fbuffer;
272 for (off = 0; off < fh->buffers.buffer_size;
Hans Verkuilb80696b2009-02-18 13:20:05 -0300273 off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300274 ClearPageReserved(virt_to_page(mem + off));
Trent Piepho1159b7f2009-03-10 23:28:16 -0300275 kfree(fh->buffers.buffer[i].v4l.fbuffer);
276 fh->buffers.buffer[i].v4l.fbuffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Trent Piepho1159b7f2009-03-10 23:28:16 -0300279 fh->buffers.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Allocate the MJPEG grab buffers.
284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * If a Natoma chipset is present and this is a revision 1 zr36057,
286 * each MJPEG buffer needs to be physically contiguous.
287 * (RJ: This statement is from Dave Perks' original driver,
288 * I could never check it because I have a zr36067)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *
290 * RJ: The contents grab buffers needs never be accessed in the driver.
291 * Therefore there is no need to allocate them with vmalloc in order
292 * to get a contiguous virtual memory space.
293 * I don't understand why many other drivers first allocate them with
294 * vmalloc (which uses internally also get_zeroed_page, but delivers you
295 * virtual addresses) and then again have to make a lot of efforts
296 * to get the physical address.
297 *
298 * Ben Capper:
299 * On big-endian architectures (such as ppc) some extra steps
300 * are needed. When reading and writing to the stat_com array
301 * and fragment buffers, the device expects to see little-
302 * endian values. The use of cpu_to_le32() and le32_to_cpu()
303 * in this function (and one or two others in zoran_device.c)
304 * ensure that these values are always stored in little-endian
305 * form, regardless of architecture. The zr36057 does Very Bad
306 * Things on big endian architectures if the stat_com array
307 * and fragment buffers are not little-endian.
308 */
309
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300310static int jpg_fbuffer_alloc(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct zoran *zr = fh->zr;
313 int i, j, off;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300314 u8 *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Trent Piepho1159b7f2009-03-10 23:28:16 -0300316 for (i = 0; i < fh->buffers.num_buffers; i++) {
317 if (fh->buffers.buffer[i].jpg.frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 dprintk(2,
319 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300320 "%s: %s - buffer %d already allocated!?\n",
321 ZR_DEVNAME(zr), __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Allocate fragment table for this buffer */
324
Trent Piepho1159b7f2009-03-10 23:28:16 -0300325 mem = (void *)get_zeroed_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (mem == 0) {
327 dprintk(1,
328 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300329 "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
330 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300331 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -ENOBUFS;
333 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300334 fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
335 fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Trent Piepho1159b7f2009-03-10 23:28:16 -0300337 if (fh->buffers.need_contiguous) {
338 mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
339 if (mem == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dprintk(1,
341 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300342 "%s: %s - kmalloc failed for buffer %d\n",
343 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300344 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -ENOBUFS;
346 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300347 fh->buffers.buffer[i].jpg.frag_tab[0] =
348 cpu_to_le32(virt_to_bus(mem));
349 fh->buffers.buffer[i].jpg.frag_tab[1] =
350 cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
351 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300352 SetPageReserved(virt_to_page(mem + off));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 } else {
Hans Verkuilb80696b2009-02-18 13:20:05 -0300354 /* jpg_bufsize is already page aligned */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300355 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
356 mem = (void *)get_zeroed_page(GFP_KERNEL);
357 if (mem == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dprintk(1,
359 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300360 "%s: %s - get_zeroed_page failed for buffer %d\n",
361 ZR_DEVNAME(zr), __func__, i);
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300362 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -ENOBUFS;
364 }
365
Trent Piepho1159b7f2009-03-10 23:28:16 -0300366 fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
367 cpu_to_le32(virt_to_bus(mem));
368 fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
369 cpu_to_le32((PAGE_SIZE >> 2) << 1);
Hans Verkuil602dd482009-02-18 17:39:45 -0300370 SetPageReserved(virt_to_page(mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Trent Piepho1159b7f2009-03-10 23:28:16 -0300373 fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 }
376
377 dprintk(4,
Trent Piephoc61402b2009-03-10 23:28:33 -0300378 KERN_DEBUG "%s: %s - %d KB allocated\n",
379 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -0300380 (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Trent Piepho1159b7f2009-03-10 23:28:16 -0300382 fh->buffers.allocated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return 0;
385}
386
387/* free the MJPEG grab buffers */
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300388static void jpg_fbuffer_free(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct zoran *zr = fh->zr;
391 int i, j, off;
392 unsigned char *mem;
Hans Verkuil602dd482009-02-18 17:39:45 -0300393 __le32 frag_tab;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300394 struct zoran_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Trent Piephoc61402b2009-03-10 23:28:33 -0300396 dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Trent Piepho1159b7f2009-03-10 23:28:16 -0300398 for (i = 0, buffer = &fh->buffers.buffer[0];
399 i < fh->buffers.num_buffers; i++, buffer++) {
400 if (!buffer->jpg.frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 continue;
402
Trent Piepho1159b7f2009-03-10 23:28:16 -0300403 if (fh->buffers.need_contiguous) {
404 frag_tab = buffer->jpg.frag_tab[0];
Hans Verkuil602dd482009-02-18 17:39:45 -0300405
406 if (frag_tab) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300407 mem = bus_to_virt(le32_to_cpu(frag_tab));
408 for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
Hans Verkuil602dd482009-02-18 17:39:45 -0300409 ClearPageReserved(virt_to_page(mem + off));
Jesper Juhlf91012102005-09-10 00:26:54 -0700410 kfree(mem);
Trent Piepho1159b7f2009-03-10 23:28:16 -0300411 buffer->jpg.frag_tab[0] = 0;
412 buffer->jpg.frag_tab[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 } else {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300415 for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
416 frag_tab = buffer->jpg.frag_tab[2 * j];
Hans Verkuil602dd482009-02-18 17:39:45 -0300417
418 if (!frag_tab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
Hans Verkuil602dd482009-02-18 17:39:45 -0300420 ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
421 free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
Trent Piepho1159b7f2009-03-10 23:28:16 -0300422 buffer->jpg.frag_tab[2 * j] = 0;
423 buffer->jpg.frag_tab[2 * j + 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
426
Trent Piepho1159b7f2009-03-10 23:28:16 -0300427 free_page((unsigned long)buffer->jpg.frag_tab);
428 buffer->jpg.frag_tab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Trent Piepho1159b7f2009-03-10 23:28:16 -0300431 fh->buffers.allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * V4L Buffer grabbing
436 */
437
438static int
Trent Piepho1159b7f2009-03-10 23:28:16 -0300439zoran_v4l_set_format (struct zoran_fh *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int width,
441 int height,
442 const struct zoran_format *format)
443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct zoran *zr = fh->zr;
445 int bpp;
446
447 /* Check size and format of the grab wanted */
448
449 if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
450 height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
451 dprintk(1,
452 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300453 "%s: %s - wrong frame size (%dx%d)\n",
454 ZR_DEVNAME(zr), __func__, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -EINVAL;
456 }
457
458 bpp = (format->depth + 7) / 8;
459
460 /* Check against available buffer size */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300461 if (height * width * bpp > fh->buffers.buffer_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dprintk(1,
463 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300464 "%s: %s - video buffer size (%d kB) is too small\n",
465 ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return -EINVAL;
467 }
468
469 /* The video front end needs 4-byte alinged line sizes */
470
471 if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
472 dprintk(1,
473 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300474 "%s: %s - wrong frame alignment\n",
475 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
477 }
478
479 fh->v4l_settings.width = width;
480 fh->v4l_settings.height = height;
481 fh->v4l_settings.format = format;
482 fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
483
484 return 0;
485}
486
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300487static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct zoran *zr = fh->zr;
490 unsigned long flags;
491 int res = 0;
492
Trent Piepho1159b7f2009-03-10 23:28:16 -0300493 if (!fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 dprintk(1,
495 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300496 "%s: %s - buffers not yet allocated\n",
497 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 res = -ENOMEM;
499 }
500
501 /* No grabbing outside the buffer range! */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300502 if (num >= fh->buffers.num_buffers || num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dprintk(1,
504 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300505 "%s: %s - buffer %d is out of range\n",
506 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 res = -EINVAL;
508 }
509
510 spin_lock_irqsave(&zr->spinlock, flags);
511
Trent Piepho1159b7f2009-03-10 23:28:16 -0300512 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (zr->v4l_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300514 zr->v4l_buffers = fh->buffers;
515 fh->buffers.active = ZORAN_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else {
517 dprintk(1,
518 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300519 "%s: %s - another session is already capturing\n",
520 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 res = -EBUSY;
522 }
523 }
524
525 /* make sure a grab isn't going on currently with this buffer */
526 if (!res) {
527 switch (zr->v4l_buffers.buffer[num].state) {
528 default:
529 case BUZ_STATE_PEND:
530 if (zr->v4l_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300531 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 zr->v4l_buffers.allocated = 0;
533 }
534 res = -EBUSY; /* what are you doing? */
535 break;
536 case BUZ_STATE_DONE:
537 dprintk(2,
538 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300539 "%s: %s - queueing buffer %d in state DONE!?\n",
540 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 case BUZ_STATE_USER:
542 /* since there is at least one unused buffer there's room for at least
543 * one more pend[] entry */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300544 zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
546 zr->v4l_buffers.buffer[num].bs.length =
547 fh->v4l_settings.bytesperline *
548 zr->v4l_settings.height;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300549 fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 }
552 }
553
554 spin_unlock_irqrestore(&zr->spinlock, flags);
555
556 if (!res && zr->v4l_buffers.active == ZORAN_FREE)
Trent Piepho1159b7f2009-03-10 23:28:16 -0300557 zr->v4l_buffers.active = fh->buffers.active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 return res;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
563 * Sync on a V4L buffer
564 */
565
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300566static int v4l_sync(struct zoran_fh *fh, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct zoran *zr = fh->zr;
569 unsigned long flags;
570
Trent Piepho1159b7f2009-03-10 23:28:16 -0300571 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dprintk(1,
573 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300574 "%s: %s - no grab active for this session\n",
575 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EINVAL;
577 }
578
579 /* check passed-in frame number */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300580 if (frame >= fh->buffers.num_buffers || frame < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dprintk(1,
Trent Piephoc61402b2009-03-10 23:28:33 -0300582 KERN_ERR "%s: %s - frame %d is invalid\n",
583 ZR_DEVNAME(zr), __func__, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -EINVAL;
585 }
586
587 /* Check if is buffer was queued at all */
588 if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
589 dprintk(1,
590 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300591 "%s: %s - attempt to sync on a buffer which was not queued?\n",
592 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -EPROTO;
594 }
595
596 /* wait on this buffer to get ready */
597 if (!wait_event_interruptible_timeout(zr->v4l_capq,
Trent Piepho1159b7f2009-03-10 23:28:16 -0300598 (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -ETIME;
600 if (signal_pending(current))
601 return -ERESTARTSYS;
602
603 /* buffer should now be in BUZ_STATE_DONE */
604 if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
605 dprintk(2,
Trent Piephoc61402b2009-03-10 23:28:33 -0300606 KERN_ERR "%s: %s - internal state error\n",
607 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300610 fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 spin_lock_irqsave(&zr->spinlock, flags);
613
614 /* Check if streaming capture has finished */
615 if (zr->v4l_pend_tail == zr->v4l_pend_head) {
616 zr36057_set_memgrab(zr, 0);
617 if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300618 fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 zr->v4l_buffers.allocated = 0;
620 }
621 }
622
623 spin_unlock_irqrestore(&zr->spinlock, flags);
624
625 return 0;
626}
627
628/*
629 * Queue a MJPEG buffer for capture/playback
630 */
631
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300632static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
633 enum zoran_codec_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct zoran *zr = fh->zr;
636 unsigned long flags;
637 int res = 0;
638
639 /* Check if buffers are allocated */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300640 if (!fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 dprintk(1,
642 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300643 "%s: %s - buffers not yet allocated\n",
644 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -ENOMEM;
646 }
647
648 /* No grabbing outside the buffer range! */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300649 if (num >= fh->buffers.num_buffers || num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dprintk(1,
651 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300652 "%s: %s - buffer %d out of range\n",
653 ZR_DEVNAME(zr), __func__, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655 }
656
657 /* what is the codec mode right now? */
658 if (zr->codec_mode == BUZ_MODE_IDLE) {
659 zr->jpg_settings = fh->jpg_settings;
660 } else if (zr->codec_mode != mode) {
661 /* wrong codec mode active - invalid */
662 dprintk(1,
663 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300664 "%s: %s - codec in wrong mode\n",
665 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
667 }
668
Trent Piepho1159b7f2009-03-10 23:28:16 -0300669 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (zr->jpg_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300671 zr->jpg_buffers = fh->buffers;
672 fh->buffers.active = ZORAN_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 } else {
674 dprintk(1,
675 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300676 "%s: %s - another session is already capturing\n",
677 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 res = -EBUSY;
679 }
680 }
681
682 if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
683 /* Ok load up the jpeg codec */
684 zr36057_enable_jpg(zr, mode);
685 }
686
Ronald S. Bultje8bc3efc2005-11-07 01:00:22 -0800687 spin_lock_irqsave(&zr->spinlock, flags);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!res) {
690 switch (zr->jpg_buffers.buffer[num].state) {
691 case BUZ_STATE_DONE:
692 dprintk(2,
693 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -0300694 "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
695 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case BUZ_STATE_USER:
697 /* since there is at least one unused buffer there's room for at
698 *least one more pend[] entry */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300699 zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300701 fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 zoran_feed_stat_com(zr);
703 break;
704 default:
705 case BUZ_STATE_DMA:
706 case BUZ_STATE_PEND:
707 if (zr->jpg_buffers.active == ZORAN_FREE) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300708 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 zr->jpg_buffers.allocated = 0;
710 }
711 res = -EBUSY; /* what are you doing? */
712 break;
713 }
714 }
715
716 spin_unlock_irqrestore(&zr->spinlock, flags);
717
Trent Piepho1159b7f2009-03-10 23:28:16 -0300718 if (!res && zr->jpg_buffers.active == ZORAN_FREE)
719 zr->jpg_buffers.active = fh->buffers.active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 return res;
722}
723
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300724static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct zoran *zr = fh->zr;
727 int res = 0;
728
729 /* Does the user want to stop streaming? */
730 if (frame < 0) {
731 if (zr->codec_mode == mode) {
Trent Piepho1159b7f2009-03-10 23:28:16 -0300732 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dprintk(1,
734 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300735 "%s: %s(-1) - session not active\n",
736 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EINVAL;
738 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300739 fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 zr->jpg_buffers.allocated = 0;
741 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
742 return 0;
743 } else {
744 dprintk(1,
745 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300746 "%s: %s - stop streaming but not in streaming mode\n",
747 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EINVAL;
749 }
750 }
751
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300752 if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return res;
754
755 /* Start the jpeg codec when the first frame is queued */
756 if (!res && zr->jpg_que_head == 1)
757 jpeg_start(zr);
758
759 return res;
760}
761
762/*
763 * Sync on a MJPEG buffer
764 */
765
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300766static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct zoran *zr = fh->zr;
769 unsigned long flags;
770 int frame;
771
Trent Piepho1159b7f2009-03-10 23:28:16 -0300772 if (fh->buffers.active == ZORAN_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dprintk(1,
774 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300775 "%s: %s - capture is not currently active\n",
776 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EINVAL;
778 }
779 if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
780 zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
781 dprintk(1,
782 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300783 "%s: %s - codec not in streaming mode\n",
784 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EINVAL;
786 }
787 if (!wait_event_interruptible_timeout(zr->jpg_capq,
788 (zr->jpg_que_tail != zr->jpg_dma_tail ||
789 zr->jpg_dma_tail == zr->jpg_dma_head),
790 10*HZ)) {
791 int isr;
792
793 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
794 udelay(1);
795 zr->codec->control(zr->codec, CODEC_G_STATUS,
796 sizeof(isr), &isr);
797 dprintk(1,
798 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300799 "%s: %s - timeout: codec isr=0x%02x\n",
800 ZR_DEVNAME(zr), __func__, isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 return -ETIME;
803
804 }
805 if (signal_pending(current))
806 return -ERESTARTSYS;
807
808 spin_lock_irqsave(&zr->spinlock, flags);
809
810 if (zr->jpg_dma_tail != zr->jpg_dma_head)
811 frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
812 else
813 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
814
815 /* buffer should now be in BUZ_STATE_DONE */
Jean Delvare18b548c2007-07-17 18:29:41 -0300816 if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
817 dprintk(2,
Trent Piephoc61402b2009-03-10 23:28:33 -0300818 KERN_ERR "%s: %s - internal state error\n",
819 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 *bs = zr->jpg_buffers.buffer[frame].bs;
822 bs->frame = frame;
823 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300824 fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 spin_unlock_irqrestore(&zr->spinlock, flags);
827
828 return 0;
829}
830
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300831static void zoran_open_init_session(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct zoran *zr = fh->zr;
835
836 /* Per default, map the V4L Buffers */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300837 map_mode_raw(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* take over the card's current settings */
840 fh->overlay_settings = zr->overlay_settings;
841 fh->overlay_settings.is_set = 0;
842 fh->overlay_settings.format = zr->overlay_settings.format;
843 fh->overlay_active = ZORAN_FREE;
844
845 /* v4l settings */
846 fh->v4l_settings = zr->v4l_settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* jpg settings */
848 fh->jpg_settings = zr->jpg_settings;
849
Trent Piepho1159b7f2009-03-10 23:28:16 -0300850 /* buffers */
851 memset(&fh->buffers, 0, sizeof(fh->buffers));
852 for (i = 0; i < MAX_FRAME; i++) {
853 fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
854 fh->buffers.buffer[i].bs.frame = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Trent Piepho1159b7f2009-03-10 23:28:16 -0300856 fh->buffers.allocated = 0;
857 fh->buffers.active = ZORAN_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300860static void zoran_close_end_session(struct zoran_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct zoran *zr = fh->zr;
863
864 /* overlay */
865 if (fh->overlay_active != ZORAN_FREE) {
866 fh->overlay_active = zr->overlay_active = ZORAN_FREE;
867 zr->v4l_overlay_active = 0;
868 if (!zr->v4l_memgrab_active)
869 zr36057_overlay(zr, 0);
870 zr->overlay_mask = NULL;
871 }
872
Trent Piepho1159b7f2009-03-10 23:28:16 -0300873 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
874 /* v4l capture */
875 if (fh->buffers.active != ZORAN_FREE) {
876 unsigned long flags;
Trent Piepho9896bbc2007-07-17 18:29:45 -0300877
Trent Piepho1159b7f2009-03-10 23:28:16 -0300878 spin_lock_irqsave(&zr->spinlock, flags);
879 zr36057_set_memgrab(zr, 0);
880 zr->v4l_buffers.allocated = 0;
881 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
882 spin_unlock_irqrestore(&zr->spinlock, flags);
883 }
884
885 /* v4l buffers */
886 if (fh->buffers.allocated)
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300887 v4l_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -0300888 } else {
889 /* jpg capture */
890 if (fh->buffers.active != ZORAN_FREE) {
891 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
892 zr->jpg_buffers.allocated = 0;
893 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
894 }
895
896 /* jpg buffers */
897 if (fh->buffers.allocated)
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300898 jpg_fbuffer_free(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*
903 * Open a zoran card. Right now the flags stuff is just playing
904 */
905
Trent Piepho601139e2009-01-12 13:09:46 -0300906static int zoran_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Trent Piepho601139e2009-01-12 13:09:46 -0300908 struct zoran *zr = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct zoran_fh *fh;
Trent Piephofc96ab72009-01-12 13:09:46 -0300910 int res, first_open = 0;
911
Trent Piephoc61402b2009-03-10 23:28:33 -0300912 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
913 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Hans Verkuild56dc612008-07-30 08:43:36 -0300915 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Trent Piephofc96ab72009-01-12 13:09:46 -0300917 if (zr->user >= 2048) {
918 dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
919 ZR_DEVNAME(zr), zr->user);
920 res = -EBUSY;
921 goto fail_unlock;
922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* now, create the open()-specific file_ops struct */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200925 fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!fh) {
927 dprintk(1,
928 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300929 "%s: %s - allocation of zoran_fh failed\n",
930 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 res = -ENOMEM;
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300932 goto fail_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
935 * on norm-change! */
936 fh->overlay_mask =
937 kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
938 if (!fh->overlay_mask) {
939 dprintk(1,
940 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -0300941 "%s: %s - allocation of overlay_mask failed\n",
942 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 res = -ENOMEM;
Trent Piephofc96ab72009-01-12 13:09:46 -0300944 goto fail_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 if (zr->user++ == 0)
948 first_open = 1;
949
Ingo Molnar384c3682006-03-22 03:54:16 -0300950 /*mutex_unlock(&zr->resource_lock);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* default setup - TODO: look at flags */
953 if (first_open) { /* First device open */
954 zr36057_restart(zr);
955 zoran_open_init_params(zr);
956 zoran_init_hardware(zr);
957
958 btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
959 }
960
961 /* set file_ops stuff */
962 file->private_data = fh;
963 fh->zr = zr;
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300964 zoran_open_init_session(fh);
Hans Verkuild56dc612008-07-30 08:43:36 -0300965 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return 0;
968
Trent Piephofc96ab72009-01-12 13:09:46 -0300969fail_fh:
970 kfree(fh);
Trent Piephofc96ab72009-01-12 13:09:46 -0300971fail_unlock:
Hans Verkuild56dc612008-07-30 08:43:36 -0300972 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Trent Piephofc96ab72009-01-12 13:09:46 -0300974 dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
975 ZR_DEVNAME(zr), res, zr->user);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return res;
978}
979
980static int
Hans Verkuilbec43662008-12-30 06:58:20 -0300981zoran_close(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct zoran_fh *fh = file->private_data;
984 struct zoran *zr = fh->zr;
985
Trent Piephoc61402b2009-03-10 23:28:33 -0300986 dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
987 ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* kernel locks (fs/device.c), so don't do that ourselves
990 * (prevents deadlocks) */
Ingo Molnar384c3682006-03-22 03:54:16 -0300991 /*mutex_lock(&zr->resource_lock);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Trent Piephoe5ee3f62009-03-10 23:28:31 -0300993 zoran_close_end_session(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (zr->user-- == 1) { /* Last process */
996 /* Clean up JPEG process */
997 wake_up_interruptible(&zr->jpg_capq);
998 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
999 zr->jpg_buffers.allocated = 0;
1000 zr->jpg_buffers.active = ZORAN_FREE;
1001
1002 /* disable interrupts */
1003 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1004
Jean Delvare18b548c2007-07-17 18:29:41 -03001005 if (zr36067_debug > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 print_interrupts(zr);
1007
1008 /* Overlay off */
1009 zr->v4l_overlay_active = 0;
1010 zr36057_overlay(zr, 0);
1011 zr->overlay_mask = NULL;
1012
1013 /* capture off */
1014 wake_up_interruptible(&zr->v4l_capq);
1015 zr36057_set_memgrab(zr, 0);
1016 zr->v4l_buffers.allocated = 0;
1017 zr->v4l_buffers.active = ZORAN_FREE;
1018 zoran_set_pci_master(zr, 0);
1019
1020 if (!pass_through) { /* Switch to color bar */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001021 decoder_call(zr, video, s_stream, 0);
Hans Verkuil5325b422009-04-02 11:26:22 -03001022 encoder_call(zr, video, s_routing, 2, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
1025
1026 file->private_data = NULL;
1027 kfree(fh->overlay_mask);
1028 kfree(fh);
1029
Trent Piephoc61402b2009-03-10 23:28:33 -03001030 dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 return 0;
1033}
1034
1035
1036static ssize_t
1037zoran_read (struct file *file,
1038 char __user *data,
1039 size_t count,
1040 loff_t *ppos)
1041{
1042 /* we simply don't support read() (yet)... */
1043
1044 return -EINVAL;
1045}
1046
1047static ssize_t
1048zoran_write (struct file *file,
1049 const char __user *data,
1050 size_t count,
1051 loff_t *ppos)
1052{
1053 /* ...and the same goes for write() */
1054
1055 return -EINVAL;
1056}
1057
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001058static int setup_fbuffer(struct zoran_fh *fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 void *base,
1060 const struct zoran_format *fmt,
1061 int width,
1062 int height,
1063 int bytesperline)
1064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct zoran *zr = fh->zr;
1066
1067 /* (Ronald) v4l/v4l2 guidelines */
1068 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1069 return -EPERM;
1070
Alan Coxe3558802006-09-14 11:47:55 -03001071 /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
1072 ALi Magik (that needs very low latency while the card needs a
1073 higher value always) */
1074
1075 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1076 return -ENXIO;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* we need a bytesperline value, even if not given */
1079 if (!bytesperline)
1080 bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
1081
1082#if 0
1083 if (zr->overlay_active) {
1084 /* dzjee... stupid users... don't even bother to turn off
1085 * overlay before changing the memory location...
1086 * normally, we would return errors here. However, one of
1087 * the tools that does this is... xawtv! and since xawtv
1088 * is used by +/- 99% of the users, we'd rather be user-
1089 * friendly and silently do as if nothing went wrong */
1090 dprintk(3,
1091 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001092 "%s: %s - forced overlay turnoff because framebuffer changed\n",
1093 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 zr36057_overlay(zr, 0);
1095 }
1096#endif
1097
1098 if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
1099 dprintk(1,
1100 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001101 "%s: %s - no valid overlay format given\n",
1102 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -EINVAL;
1104 }
1105 if (height <= 0 || width <= 0 || bytesperline <= 0) {
1106 dprintk(1,
1107 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001108 "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
1109 ZR_DEVNAME(zr), __func__, width, height, bytesperline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return -EINVAL;
1111 }
1112 if (bytesperline & 3) {
1113 dprintk(1,
1114 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001115 "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
1116 ZR_DEVNAME(zr), __func__, bytesperline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -EINVAL;
1118 }
1119
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001120 zr->vbuf_base = (void *) ((unsigned long) base & ~3);
1121 zr->vbuf_height = height;
1122 zr->vbuf_width = width;
1123 zr->vbuf_depth = fmt->depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 zr->overlay_settings.format = fmt;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001125 zr->vbuf_bytesperline = bytesperline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* The user should set new window parameters */
1128 zr->overlay_settings.is_set = 0;
1129
1130 return 0;
1131}
1132
1133
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001134static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
1135 struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct zoran *zr = fh->zr;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001138 struct v4l2_clip *vcp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int on, end;
1140
1141
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001142 if (!zr->vbuf_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 dprintk(1,
1144 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001145 "%s: %s - frame buffer has to be set first\n",
1146 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EINVAL;
1148 }
1149
1150 if (!fh->overlay_settings.format) {
1151 dprintk(1,
1152 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001153 "%s: %s - no overlay format set\n",
1154 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EINVAL;
1156 }
1157
1158 /*
1159 * The video front end needs 4-byte alinged line sizes, we correct that
1160 * silently here if necessary
1161 */
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001162 if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 end = (x + width) & ~1; /* round down */
1164 x = (x + 1) & ~1; /* round up */
1165 width = end - x;
1166 }
1167
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001168 if (zr->vbuf_depth == 24) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 end = (x + width) & ~3; /* round down */
1170 x = (x + 3) & ~3; /* round up */
1171 width = end - x;
1172 }
1173
1174 if (width > BUZ_MAX_WIDTH)
1175 width = BUZ_MAX_WIDTH;
1176 if (height > BUZ_MAX_HEIGHT)
1177 height = BUZ_MAX_HEIGHT;
1178
1179 /* Check for vaild parameters */
1180 if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
1181 width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
1182 dprintk(1,
1183 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001184 "%s: %s - width = %d or height = %d invalid\n",
1185 ZR_DEVNAME(zr), __func__, width, height);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return -EINVAL;
1187 }
1188
1189 fh->overlay_settings.x = x;
1190 fh->overlay_settings.y = y;
1191 fh->overlay_settings.width = width;
1192 fh->overlay_settings.height = height;
1193 fh->overlay_settings.clipcount = clipcount;
1194
1195 /*
1196 * If an overlay is running, we have to switch it off
1197 * and switch it on again in order to get the new settings in effect.
1198 *
1199 * We also want to avoid that the overlay mask is written
1200 * when an overlay is running.
1201 */
1202
1203 on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
1204 zr->overlay_active != ZORAN_FREE &&
1205 fh->overlay_active != ZORAN_FREE;
1206 if (on)
1207 zr36057_overlay(zr, 0);
1208
1209 /*
1210 * Write the overlay mask if clips are wanted.
1211 * We prefer a bitmap.
1212 */
1213 if (bitmap) {
1214 /* fake value - it just means we want clips */
1215 fh->overlay_settings.clipcount = 1;
1216
1217 if (copy_from_user(fh->overlay_mask, bitmap,
1218 (width * height + 7) / 8)) {
1219 return -EFAULT;
1220 }
1221 } else if (clipcount > 0) {
1222 /* write our own bitmap from the clips */
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001223 vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (vcp == NULL) {
1225 dprintk(1,
1226 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001227 "%s: %s - Alloc of clip mask failed\n",
1228 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -ENOMEM;
1230 }
1231 if (copy_from_user
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001232 (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 vfree(vcp);
1234 return -EFAULT;
1235 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001236 write_overlay_mask(fh, vcp, clipcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 vfree(vcp);
1238 }
1239
1240 fh->overlay_settings.is_set = 1;
1241 if (fh->overlay_active != ZORAN_FREE &&
1242 zr->overlay_active != ZORAN_FREE)
1243 zr->overlay_settings = fh->overlay_settings;
1244
1245 if (on)
1246 zr36057_overlay(zr, 1);
1247
1248 /* Make sure the changes come into effect */
1249 return wait_grab_pending(zr);
1250}
1251
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001252static int setup_overlay(struct zoran_fh *fh, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct zoran *zr = fh->zr;
1255
1256 /* If there is nothing to do, return immediatly */
1257 if ((on && fh->overlay_active != ZORAN_FREE) ||
1258 (!on && fh->overlay_active == ZORAN_FREE))
1259 return 0;
1260
1261 /* check whether we're touching someone else's overlay */
1262 if (on && zr->overlay_active != ZORAN_FREE &&
1263 fh->overlay_active == ZORAN_FREE) {
1264 dprintk(1,
1265 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001266 "%s: %s - overlay is already active for another session\n",
1267 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -EBUSY;
1269 }
1270 if (!on && zr->overlay_active != ZORAN_FREE &&
1271 fh->overlay_active == ZORAN_FREE) {
1272 dprintk(1,
1273 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001274 "%s: %s - you cannot cancel someone else's session\n",
1275 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -EPERM;
1277 }
1278
1279 if (on == 0) {
1280 zr->overlay_active = fh->overlay_active = ZORAN_FREE;
1281 zr->v4l_overlay_active = 0;
1282 /* When a grab is running, the video simply
1283 * won't be switched on any more */
1284 if (!zr->v4l_memgrab_active)
1285 zr36057_overlay(zr, 0);
1286 zr->overlay_mask = NULL;
1287 } else {
Hans Verkuil7f6adea2009-02-19 17:31:17 -03001288 if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 dprintk(1,
1290 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001291 "%s: %s - buffer or window not set\n",
1292 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return -EINVAL;
1294 }
1295 if (!fh->overlay_settings.format) {
1296 dprintk(1,
1297 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001298 "%s: %s - no overlay format set\n",
1299 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EINVAL;
1301 }
1302 zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
1303 zr->v4l_overlay_active = 1;
1304 zr->overlay_mask = fh->overlay_mask;
1305 zr->overlay_settings = fh->overlay_settings;
1306 if (!zr->v4l_memgrab_active)
1307 zr36057_overlay(zr, 1);
1308 /* When a grab is running, the video will be
1309 * switched on when grab is finished */
1310 }
1311
1312 /* Make sure the changes come into effect */
1313 return wait_grab_pending(zr);
1314}
1315
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001316/* get the status of a buffer in the clients buffer queue */
1317static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
1318 struct v4l2_buffer *buf, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct zoran *zr = fh->zr;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001321 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 buf->flags = V4L2_BUF_FLAG_MAPPED;
1324
1325 switch (fh->map_mode) {
1326 case ZORAN_MAP_MODE_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* check range */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001328 if (num < 0 || num >= fh->buffers.num_buffers ||
1329 !fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 dprintk(1,
1331 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001332 "%s: %s - wrong number or buffers not allocated\n",
1333 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EINVAL;
1335 }
1336
Trent Piepho1159b7f2009-03-10 23:28:16 -03001337 spin_lock_irqsave(&zr->spinlock, flags);
1338 dprintk(3,
1339 KERN_DEBUG
1340 "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
1341 ZR_DEVNAME(zr), __func__,
1342 "FAL"[fh->buffers.active], num,
1343 "UPMD"[zr->v4l_buffers.buffer[num].state],
1344 fh->buffers.buffer[num].map ? 'Y' : 'N');
1345 spin_unlock_irqrestore(&zr->spinlock, flags);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001348 buf->length = fh->buffers.buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /* get buffer */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001351 buf->bytesused = fh->buffers.buffer[num].bs.length;
1352 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1353 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1354 buf->sequence = fh->buffers.buffer[num].bs.seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 buf->flags |= V4L2_BUF_FLAG_DONE;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001356 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 } else {
1358 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1359 }
1360
1361 if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
1362 buf->field = V4L2_FIELD_TOP;
1363 else
1364 buf->field = V4L2_FIELD_INTERLACED;
1365
1366 break;
1367
1368 case ZORAN_MAP_MODE_JPG_REC:
1369 case ZORAN_MAP_MODE_JPG_PLAY:
1370
1371 /* check range */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001372 if (num < 0 || num >= fh->buffers.num_buffers ||
1373 !fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dprintk(1,
1375 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001376 "%s: %s - wrong number or buffers not allocated\n",
1377 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -EINVAL;
1379 }
1380
1381 buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
1382 V4L2_BUF_TYPE_VIDEO_CAPTURE :
1383 V4L2_BUF_TYPE_VIDEO_OUTPUT;
Trent Piepho1159b7f2009-03-10 23:28:16 -03001384 buf->length = fh->buffers.buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* these variables are only written after frame has been captured */
Trent Piepho1159b7f2009-03-10 23:28:16 -03001387 if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
1388 fh->buffers.buffer[num].state == BUZ_STATE_USER) {
1389 buf->sequence = fh->buffers.buffer[num].bs.seq;
1390 buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
1391 buf->bytesused = fh->buffers.buffer[num].bs.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 buf->flags |= V4L2_BUF_FLAG_DONE;
1393 } else {
1394 buf->flags |= V4L2_BUF_FLAG_QUEUED;
1395 }
1396
1397 /* which fields are these? */
1398 if (fh->jpg_settings.TmpDcm != 1)
Trent Piepho1159b7f2009-03-10 23:28:16 -03001399 buf->field = fh->jpg_settings.odd_even ?
1400 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 else
Trent Piepho1159b7f2009-03-10 23:28:16 -03001402 buf->field = fh->jpg_settings.odd_even ?
1403 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 break;
1406
1407 default:
1408
1409 dprintk(5,
1410 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001411 "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
1412 ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EINVAL;
1414 }
1415
1416 buf->memory = V4L2_MEMORY_MMAP;
1417 buf->index = num;
1418 buf->m.offset = buf->length * num;
1419
1420 return 0;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423static int
1424zoran_set_norm (struct zoran *zr,
Hans Verkuil107063c2009-02-18 17:26:06 -03001425 v4l2_std_id norm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Hans Verkuil107063c2009-02-18 17:26:06 -03001427 int on;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (zr->v4l_buffers.active != ZORAN_FREE ||
1430 zr->jpg_buffers.active != ZORAN_FREE) {
1431 dprintk(1,
1432 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -03001433 "%s: %s called while in playback/capture mode\n",
1434 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -EBUSY;
1436 }
1437
Hans Verkuil107063c2009-02-18 17:26:06 -03001438 if (!(norm & zr->card.norms)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 dprintk(1,
Trent Piephoc61402b2009-03-10 23:28:33 -03001440 KERN_ERR "%s: %s - unsupported norm %llx\n",
1441 ZR_DEVNAME(zr), __func__, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
1443 }
1444
Hans Verkuil107063c2009-02-18 17:26:06 -03001445 if (norm == V4L2_STD_ALL) {
1446 int status = 0;
1447 v4l2_std_id std = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001449 decoder_call(zr, video, querystd, &std);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001450 decoder_call(zr, core, s_std, std);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* let changes come into effect */
1453 ssleep(2);
1454
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001455 decoder_call(zr, video, g_input_status, &status);
Hans Verkuil107063c2009-02-18 17:26:06 -03001456 if (status & V4L2_IN_ST_NO_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 dprintk(1,
1458 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001459 "%s: %s - no norm detected\n",
1460 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* reset norm */
Hans Verkuilf41737e2009-04-01 03:52:39 -03001462 decoder_call(zr, core, s_std, zr->norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EIO;
1464 }
1465
Hans Verkuil107063c2009-02-18 17:26:06 -03001466 norm = std;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Hans Verkuil107063c2009-02-18 17:26:06 -03001468 if (norm & V4L2_STD_SECAM)
1469 zr->timing = zr->card.tvn[2];
1470 else if (norm & V4L2_STD_NTSC)
1471 zr->timing = zr->card.tvn[1];
1472 else
1473 zr->timing = zr->card.tvn[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /* We switch overlay off and on since a change in the
1476 * norm needs different VFE settings */
1477 on = zr->overlay_active && !zr->v4l_memgrab_active;
1478 if (on)
1479 zr36057_overlay(zr, 0);
1480
Hans Verkuilf41737e2009-04-01 03:52:39 -03001481 decoder_call(zr, core, s_std, norm);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001482 encoder_call(zr, video, s_std_output, norm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (on)
1485 zr36057_overlay(zr, 1);
1486
1487 /* Make sure the changes come into effect */
1488 zr->norm = norm;
1489
1490 return 0;
1491}
1492
1493static int
1494zoran_set_input (struct zoran *zr,
1495 int input)
1496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (input == zr->input) {
1498 return 0;
1499 }
1500
1501 if (zr->v4l_buffers.active != ZORAN_FREE ||
1502 zr->jpg_buffers.active != ZORAN_FREE) {
1503 dprintk(1,
1504 KERN_WARNING
Trent Piephoc61402b2009-03-10 23:28:33 -03001505 "%s: %s called while in playback/capture mode\n",
1506 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EBUSY;
1508 }
1509
1510 if (input < 0 || input >= zr->card.inputs) {
1511 dprintk(1,
1512 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03001513 "%s: %s - unnsupported input %d\n",
1514 ZR_DEVNAME(zr), __func__, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
1516 }
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 zr->input = input;
1519
Hans Verkuil5325b422009-04-02 11:26:22 -03001520 decoder_call(zr, video, s_routing,
1521 zr->card.input[input].muxsel, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 return 0;
1524}
1525
1526/*
1527 * ioctl routine
1528 */
1529
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001530#ifdef CONFIG_VIDEO_V4L1_COMPAT
Hans Verkuil96b8e142009-02-18 13:13:31 -03001531static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Hans Verkuil96b8e142009-02-18 13:13:31 -03001533 struct zoran_fh *fh = __fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct zoran *zr = fh->zr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct zoran_jpg_settings settings;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 case BUZIOC_G_PARAMS:
1539 {
1540 struct zoran_params *bparams = arg;
1541
1542 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
1543
1544 memset(bparams, 0, sizeof(struct zoran_params));
1545 bparams->major_version = MAJOR_VERSION;
1546 bparams->minor_version = MINOR_VERSION;
1547
Ingo Molnar384c3682006-03-22 03:54:16 -03001548 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Hans Verkuil107063c2009-02-18 17:26:06 -03001550 if (zr->norm & V4L2_STD_NTSC)
1551 bparams->norm = VIDEO_MODE_NTSC;
1552 else if (zr->norm & V4L2_STD_PAL)
1553 bparams->norm = VIDEO_MODE_PAL;
1554 else
1555 bparams->norm = VIDEO_MODE_SECAM;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 bparams->input = zr->input;
1558
1559 bparams->decimation = fh->jpg_settings.decimation;
1560 bparams->HorDcm = fh->jpg_settings.HorDcm;
1561 bparams->VerDcm = fh->jpg_settings.VerDcm;
1562 bparams->TmpDcm = fh->jpg_settings.TmpDcm;
1563 bparams->field_per_buff = fh->jpg_settings.field_per_buff;
1564 bparams->img_x = fh->jpg_settings.img_x;
1565 bparams->img_y = fh->jpg_settings.img_y;
1566 bparams->img_width = fh->jpg_settings.img_width;
1567 bparams->img_height = fh->jpg_settings.img_height;
1568 bparams->odd_even = fh->jpg_settings.odd_even;
1569
1570 bparams->quality = fh->jpg_settings.jpg_comp.quality;
1571 bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
1572 bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
1573 memcpy(bparams->APP_data,
1574 fh->jpg_settings.jpg_comp.APP_data,
1575 sizeof(bparams->APP_data));
1576 bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
1577 memcpy(bparams->COM_data,
1578 fh->jpg_settings.jpg_comp.COM_data,
1579 sizeof(bparams->COM_data));
1580 bparams->jpeg_markers =
1581 fh->jpg_settings.jpg_comp.jpeg_markers;
1582
Ingo Molnar384c3682006-03-22 03:54:16 -03001583 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 bparams->VFIFO_FB = 0;
1586
1587 return 0;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 case BUZIOC_S_PARAMS:
1591 {
1592 struct zoran_params *bparams = arg;
1593 int res = 0;
1594
1595 dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
1596
1597 settings.decimation = bparams->decimation;
1598 settings.HorDcm = bparams->HorDcm;
1599 settings.VerDcm = bparams->VerDcm;
1600 settings.TmpDcm = bparams->TmpDcm;
1601 settings.field_per_buff = bparams->field_per_buff;
1602 settings.img_x = bparams->img_x;
1603 settings.img_y = bparams->img_y;
1604 settings.img_width = bparams->img_width;
1605 settings.img_height = bparams->img_height;
1606 settings.odd_even = bparams->odd_even;
1607
1608 settings.jpg_comp.quality = bparams->quality;
1609 settings.jpg_comp.APPn = bparams->APPn;
1610 settings.jpg_comp.APP_len = bparams->APP_len;
1611 memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
1612 sizeof(bparams->APP_data));
1613 settings.jpg_comp.COM_len = bparams->COM_len;
1614 memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
1615 sizeof(bparams->COM_data));
1616 settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
1617
Ingo Molnar384c3682006-03-22 03:54:16 -03001618 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (zr->codec_mode != BUZ_MODE_IDLE) {
1621 dprintk(1,
1622 KERN_ERR
1623 "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
1624 ZR_DEVNAME(zr));
1625 res = -EINVAL;
1626 goto sparams_unlock_and_return;
1627 }
1628
1629 /* Check the params first before overwriting our
1630 * nternal values */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03001631 if (zoran_check_jpg_settings(zr, &settings, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 res = -EINVAL;
1633 goto sparams_unlock_and_return;
1634 }
1635
1636 fh->jpg_settings = settings;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001637sparams_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001638 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 return res;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 case BUZIOC_REQBUFS:
1644 {
1645 struct zoran_requestbuffers *breq = arg;
1646 int res = 0;
1647
1648 dprintk(3,
1649 KERN_DEBUG
1650 "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
1651 ZR_DEVNAME(zr), breq->count, breq->size);
1652
1653 /* Enforce reasonable lower and upper limits */
1654 if (breq->count < 4)
1655 breq->count = 4; /* Could be choosen smaller */
1656 if (breq->count > jpg_nbufs)
1657 breq->count = jpg_nbufs;
1658 breq->size = PAGE_ALIGN(breq->size);
1659 if (breq->size < 8192)
1660 breq->size = 8192; /* Arbitrary */
1661 /* breq->size is limited by 1 page for the stat_com
1662 * tables to a Maximum of 2 MB */
1663 if (breq->size > jpg_bufsize)
1664 breq->size = jpg_bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Ingo Molnar384c3682006-03-22 03:54:16 -03001666 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Trent Piepho1159b7f2009-03-10 23:28:16 -03001668 if (fh->buffers.allocated) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 dprintk(1,
1670 KERN_ERR
Hans Verkuilb80696b2009-02-18 13:20:05 -03001671 "%s: BUZIOC_REQBUFS - buffers already allocated\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ZR_DEVNAME(zr));
1673 res = -EBUSY;
1674 goto jpgreqbuf_unlock_and_return;
1675 }
1676
Trent Piepho1159b7f2009-03-10 23:28:16 -03001677 /* The next mmap will map the MJPEG buffers - could
1678 * also be *_PLAY, but it doesn't matter here */
1679 map_mode_jpg(fh, 0);
1680 fh->buffers.num_buffers = breq->count;
1681 fh->buffers.buffer_size = breq->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001683 if (jpg_fbuffer_alloc(fh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 res = -ENOMEM;
1685 goto jpgreqbuf_unlock_and_return;
1686 }
1687
Hans Verkuil96b8e142009-02-18 13:13:31 -03001688jpgreqbuf_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001689 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 return res;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 case BUZIOC_QBUF_CAPT:
1695 {
1696 int *frame = arg, res;
1697
1698 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
1699 ZR_DEVNAME(zr), *frame);
1700
Ingo Molnar384c3682006-03-22 03:54:16 -03001701 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001702 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
Ingo Molnar384c3682006-03-22 03:54:16 -03001703 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 return res;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 case BUZIOC_QBUF_PLAY:
1709 {
1710 int *frame = arg, res;
1711
1712 dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
1713 ZR_DEVNAME(zr), *frame);
1714
Ingo Molnar384c3682006-03-22 03:54:16 -03001715 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001716 res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
Ingo Molnar384c3682006-03-22 03:54:16 -03001717 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 return res;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 case BUZIOC_SYNC:
1723 {
1724 struct zoran_sync *bsync = arg;
1725 int res;
1726
1727 dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
1728
Ingo Molnar384c3682006-03-22 03:54:16 -03001729 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03001730
1731 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
1732 dprintk(2, KERN_WARNING
1733 "%s: %s - not in jpg capture mode\n",
1734 ZR_DEVNAME(zr), __func__);
1735 res = -EINVAL;
1736 } else {
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001737 res = jpg_sync(fh, bsync);
Trent Piepho1159b7f2009-03-10 23:28:16 -03001738 }
Ingo Molnar384c3682006-03-22 03:54:16 -03001739 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 return res;
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 case BUZIOC_G_STATUS:
1745 {
1746 struct zoran_status *bstat = arg;
Hans Verkuil107063c2009-02-18 17:26:06 -03001747 int status = 0, res = 0;
1748 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
1751
1752 if (zr->codec_mode != BUZ_MODE_IDLE) {
1753 dprintk(1,
1754 KERN_ERR
1755 "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
1756 ZR_DEVNAME(zr));
1757 return -EINVAL;
1758 }
1759
Ingo Molnar384c3682006-03-22 03:54:16 -03001760 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (zr->codec_mode != BUZ_MODE_IDLE) {
1763 dprintk(1,
1764 KERN_ERR
1765 "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
1766 ZR_DEVNAME(zr));
1767 res = -EINVAL;
1768 goto gstat_unlock_and_return;
1769 }
1770
Hans Verkuil5325b422009-04-02 11:26:22 -03001771 decoder_call(zr, video, s_routing,
1772 zr->card.input[bstat->input].muxsel, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /* sleep 1 second */
1775 ssleep(1);
1776
1777 /* Get status of video decoder */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03001778 decoder_call(zr, video, querystd, &norm);
1779 decoder_call(zr, video, g_input_status, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* restore previous input and norm */
Hans Verkuil5325b422009-04-02 11:26:22 -03001782 decoder_call(zr, video, s_routing,
1783 zr->card.input[zr->input].muxsel, 0, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03001784gstat_unlock_and_return:
Ingo Molnar384c3682006-03-22 03:54:16 -03001785 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (!res) {
1788 bstat->signal =
Hans Verkuil107063c2009-02-18 17:26:06 -03001789 (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
1790 if (norm & V4L2_STD_NTSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 bstat->norm = VIDEO_MODE_NTSC;
Hans Verkuil107063c2009-02-18 17:26:06 -03001792 else if (norm & V4L2_STD_SECAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 bstat->norm = VIDEO_MODE_SECAM;
1794 else
1795 bstat->norm = VIDEO_MODE_PAL;
1796
1797 bstat->color =
Hans Verkuil107063c2009-02-18 17:26:06 -03001798 (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
1801 return res;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 default:
Hans Verkuil96b8e142009-02-18 13:13:31 -03001805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03001807}
1808
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001809static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
1810{
1811 struct zoran_fh *fh = __fh;
1812 struct zoran *zr = fh->zr;
1813 int i, res = 0;
1814
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001815
1816 mutex_lock(&zr->resource_lock);
1817
Trent Piepho1159b7f2009-03-10 23:28:16 -03001818 if (fh->buffers.allocated) {
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001819 dprintk(1,
1820 KERN_ERR
1821 "%s: VIDIOCGMBUF - buffers already allocated\n",
1822 ZR_DEVNAME(zr));
1823 res = -EINVAL;
1824 goto v4l1reqbuf_unlock_and_return;
1825 }
1826
Trent Piepho1159b7f2009-03-10 23:28:16 -03001827 /* The next mmap will map the V4L buffers */
1828 map_mode_raw(fh);
1829
Trent Piephoe5ee3f62009-03-10 23:28:31 -03001830 if (v4l_fbuffer_alloc(fh)) {
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001831 res = -ENOMEM;
1832 goto v4l1reqbuf_unlock_and_return;
1833 }
1834
Trent Piepho1159b7f2009-03-10 23:28:16 -03001835 vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
1836 vmbuf->frames = fh->buffers.num_buffers;
1837 for (i = 0; i < vmbuf->frames; i++)
1838 vmbuf->offsets[i] = i * fh->buffers.buffer_size;
1839
Hans Verkuildcbd83b2009-02-18 13:51:13 -03001840v4l1reqbuf_unlock_and_return:
1841 mutex_unlock(&zr->resource_lock);
1842
1843 return res;
1844}
1845#endif
1846
Hans Verkuil96b8e142009-02-18 13:13:31 -03001847static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
1848{
1849 struct zoran_fh *fh = __fh;
1850 struct zoran *zr = fh->zr;
1851
1852 memset(cap, 0, sizeof(*cap));
1853 strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
1854 strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
1855 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
1856 pci_name(zr->pci_dev));
Hans Verkuil602dd482009-02-18 17:39:45 -03001857 cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
Hans Verkuil96b8e142009-02-18 13:13:31 -03001858 RELEASE_VERSION);
Hans Verkuil602dd482009-02-18 17:39:45 -03001859 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1860 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863
Hans Verkuil96b8e142009-02-18 13:13:31 -03001864static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Trent Piepho171f48e2009-04-30 21:03:34 -03001866 unsigned int num, i;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001867
Trent Piepho171f48e2009-04-30 21:03:34 -03001868 for (num = i = 0; i < NUM_FORMATS; i++) {
1869 if (zoran_formats[i].flags & flag && num++ == fmt->index) {
1870 strncpy(fmt->description, zoran_formats[i].name,
1871 sizeof(fmt->description) - 1);
1872 /* fmt struct pre-zeroed, so adding '\0' not neeed */
1873 fmt->pixelformat = zoran_formats[i].fourcc;
1874 if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
1875 fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
1876 return 0;
1877 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03001878 }
Trent Piepho171f48e2009-04-30 21:03:34 -03001879 return -EINVAL;
Hans Verkuil96b8e142009-02-18 13:13:31 -03001880}
1881
1882static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
1883 struct v4l2_fmtdesc *f)
1884{
1885 struct zoran_fh *fh = __fh;
1886 struct zoran *zr = fh->zr;
1887
1888 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
1889}
1890
1891static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
1892 struct v4l2_fmtdesc *f)
1893{
1894 struct zoran_fh *fh = __fh;
1895 struct zoran *zr = fh->zr;
1896
1897 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
1898}
1899
1900static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
1901 struct v4l2_fmtdesc *f)
1902{
1903 struct zoran_fh *fh = __fh;
1904 struct zoran *zr = fh->zr;
1905
1906 return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
1907}
1908
1909static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
1910 struct v4l2_format *fmt)
1911{
1912 struct zoran_fh *fh = __fh;
1913 struct zoran *zr = fh->zr;
1914
1915 mutex_lock(&zr->resource_lock);
1916
1917 fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
Hans Verkuil5ca75b02009-02-18 17:12:34 -03001918 fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
Hans Verkuil96b8e142009-02-18 13:13:31 -03001919 (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
1920 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
1921 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
1922 if (fh->jpg_settings.TmpDcm == 1)
1923 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
Hans Verkuil3e667932009-02-18 13:24:56 -03001924 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
Hans Verkuil96b8e142009-02-18 13:13:31 -03001925 else
1926 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
1927 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
1928 fmt->fmt.pix.bytesperline = 0;
1929 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1930
1931 mutex_unlock(&zr->resource_lock);
1932 return 0;
1933}
1934
1935static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
1936 struct v4l2_format *fmt)
1937{
1938 struct zoran_fh *fh = __fh;
1939 struct zoran *zr = fh->zr;
1940
1941 if (fh->map_mode != ZORAN_MAP_MODE_RAW)
1942 return zoran_g_fmt_vid_out(file, fh, fmt);
1943
1944 mutex_lock(&zr->resource_lock);
1945 fmt->fmt.pix.width = fh->v4l_settings.width;
1946 fmt->fmt.pix.height = fh->v4l_settings.height;
1947 fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
1948 fh->v4l_settings.height;
1949 fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
1950 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
1951 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
1952 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
1953 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
1954 else
1955 fmt->fmt.pix.field = V4L2_FIELD_TOP;
1956 mutex_unlock(&zr->resource_lock);
1957 return 0;
1958}
1959
1960static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
1961 struct v4l2_format *fmt)
1962{
1963 struct zoran_fh *fh = __fh;
1964 struct zoran *zr = fh->zr;
1965
1966 mutex_lock(&zr->resource_lock);
1967
1968 fmt->fmt.win.w.left = fh->overlay_settings.x;
1969 fmt->fmt.win.w.top = fh->overlay_settings.y;
1970 fmt->fmt.win.w.width = fh->overlay_settings.width;
1971 fmt->fmt.win.w.height = fh->overlay_settings.height;
1972 if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
1973 fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
1974 else
1975 fmt->fmt.win.field = V4L2_FIELD_TOP;
1976
1977 mutex_unlock(&zr->resource_lock);
1978 return 0;
1979}
1980
1981static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
1982 struct v4l2_format *fmt)
1983{
1984 struct zoran_fh *fh = __fh;
1985 struct zoran *zr = fh->zr;
1986
1987 mutex_lock(&zr->resource_lock);
1988
1989 if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
1990 fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
1991 if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
1992 fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
1993 if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
1994 fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
1995 if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
1996 fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
1997
1998 mutex_unlock(&zr->resource_lock);
1999 return 0;
2000}
2001
2002static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
2003 struct v4l2_format *fmt)
2004{
2005 struct zoran_fh *fh = __fh;
2006 struct zoran *zr = fh->zr;
2007 struct zoran_jpg_settings settings;
2008 int res = 0;
2009
Hans Verkuil96b8e142009-02-18 13:13:31 -03002010 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2011 return -EINVAL;
2012
2013 mutex_lock(&zr->resource_lock);
2014 settings = fh->jpg_settings;
2015
2016 /* we actually need to set 'real' parameters now */
2017 if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
2018 settings.TmpDcm = 1;
2019 else
2020 settings.TmpDcm = 2;
2021 settings.decimation = 0;
2022 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2023 settings.VerDcm = 2;
2024 else
2025 settings.VerDcm = 1;
2026 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2027 settings.HorDcm = 4;
2028 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2029 settings.HorDcm = 2;
2030 else
2031 settings.HorDcm = 1;
2032 if (settings.TmpDcm == 1)
2033 settings.field_per_buff = 2;
2034 else
2035 settings.field_per_buff = 1;
2036
Hans Verkuil886fe232009-02-18 18:34:55 -03002037 if (settings.HorDcm > 1) {
2038 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2039 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2040 } else {
2041 settings.img_x = 0;
2042 settings.img_width = BUZ_MAX_WIDTH;
2043 }
2044
Hans Verkuil96b8e142009-02-18 13:13:31 -03002045 /* check */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002046 res = zoran_check_jpg_settings(zr, &settings, 1);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002047 if (res)
2048 goto tryfmt_unlock_and_return;
2049
2050 /* tell the user what we actually did */
2051 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2052 fmt->fmt.pix.height = settings.img_height * 2 /
2053 (settings.TmpDcm * settings.VerDcm);
2054 if (settings.TmpDcm == 1)
2055 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2056 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2057 else
2058 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2059 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
2060
2061 fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
Hans Verkuil886fe232009-02-18 18:34:55 -03002062 fmt->fmt.pix.bytesperline = 0;
2063 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002064tryfmt_unlock_and_return:
2065 mutex_unlock(&zr->resource_lock);
2066 return res;
2067}
2068
2069static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
2070 struct v4l2_format *fmt)
2071{
2072 struct zoran_fh *fh = __fh;
2073 struct zoran *zr = fh->zr;
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002074 int bpp;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002075 int i;
2076
Hans Verkuil96b8e142009-02-18 13:13:31 -03002077 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2078 return zoran_try_fmt_vid_out(file, fh, fmt);
2079
2080 mutex_lock(&zr->resource_lock);
2081
2082 for (i = 0; i < NUM_FORMATS; i++)
2083 if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
2084 break;
2085
2086 if (i == NUM_FORMATS) {
2087 mutex_unlock(&zr->resource_lock);
2088 return -EINVAL;
2089 }
2090
Trent Piepho728f5b92009-05-30 21:45:46 -03002091 bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
2092 v4l_bound_align_image(
2093 &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
2094 &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002095 mutex_unlock(&zr->resource_lock);
2096
2097 return 0;
2098}
2099
2100static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
2101 struct v4l2_format *fmt)
2102{
2103 struct zoran_fh *fh = __fh;
2104 struct zoran *zr = fh->zr;
2105 int res;
2106
2107 dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
2108 fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2109 fmt->fmt.win.w.width,
2110 fmt->fmt.win.w.height,
2111 fmt->fmt.win.clipcount,
2112 fmt->fmt.win.bitmap);
2113 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002114 res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
2115 fmt->fmt.win.w.width, fmt->fmt.win.w.height,
2116 (struct v4l2_clip __user *)fmt->fmt.win.clips,
2117 fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002118 mutex_unlock(&zr->resource_lock);
2119 return res;
2120}
2121
2122static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
2123 struct v4l2_format *fmt)
2124{
2125 struct zoran_fh *fh = __fh;
2126 struct zoran *zr = fh->zr;
2127 __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
2128 struct zoran_jpg_settings settings;
2129 int res = 0;
2130
2131 dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
2132 fmt->fmt.pix.width, fmt->fmt.pix.height,
2133 fmt->fmt.pix.pixelformat,
2134 (char *) &printformat);
2135 if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
2136 return -EINVAL;
2137
2138 mutex_lock(&zr->resource_lock);
2139
Trent Piepho1159b7f2009-03-10 23:28:16 -03002140 if (fh->buffers.allocated) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002141 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
Trent Piepho1159b7f2009-03-10 23:28:16 -03002142 ZR_DEVNAME(zr));
Hans Verkuil96b8e142009-02-18 13:13:31 -03002143 res = -EBUSY;
2144 goto sfmtjpg_unlock_and_return;
2145 }
2146
Trent Piepho1159b7f2009-03-10 23:28:16 -03002147 settings = fh->jpg_settings;
2148
Hans Verkuil96b8e142009-02-18 13:13:31 -03002149 /* we actually need to set 'real' parameters now */
Hans Verkuil7f37cc92009-02-18 17:00:37 -03002150 if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002151 settings.TmpDcm = 1;
2152 else
2153 settings.TmpDcm = 2;
2154 settings.decimation = 0;
2155 if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
2156 settings.VerDcm = 2;
2157 else
2158 settings.VerDcm = 1;
2159 if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
2160 settings.HorDcm = 4;
2161 else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
2162 settings.HorDcm = 2;
2163 else
2164 settings.HorDcm = 1;
2165 if (settings.TmpDcm == 1)
2166 settings.field_per_buff = 2;
2167 else
2168 settings.field_per_buff = 1;
2169
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002170 if (settings.HorDcm > 1) {
2171 settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
2172 settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
2173 } else {
2174 settings.img_x = 0;
2175 settings.img_width = BUZ_MAX_WIDTH;
2176 }
2177
Hans Verkuil96b8e142009-02-18 13:13:31 -03002178 /* check */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002179 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002180 if (res)
2181 goto sfmtjpg_unlock_and_return;
2182
2183 /* it's ok, so set them */
2184 fh->jpg_settings = settings;
2185
Trent Piepho1159b7f2009-03-10 23:28:16 -03002186 map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2187 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
2188
Hans Verkuil96b8e142009-02-18 13:13:31 -03002189 /* tell the user what we actually did */
2190 fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
2191 fmt->fmt.pix.height = settings.img_height * 2 /
2192 (settings.TmpDcm * settings.VerDcm);
2193 if (settings.TmpDcm == 1)
2194 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2195 V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
2196 else
2197 fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
2198 V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002199 fmt->fmt.pix.bytesperline = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002200 fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002201 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2202
Hans Verkuil96b8e142009-02-18 13:13:31 -03002203sfmtjpg_unlock_and_return:
2204 mutex_unlock(&zr->resource_lock);
2205 return res;
2206}
2207
2208static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
2209 struct v4l2_format *fmt)
2210{
2211 struct zoran_fh *fh = __fh;
2212 struct zoran *zr = fh->zr;
2213 int i;
2214 int res = 0;
2215
2216 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
2217 return zoran_s_fmt_vid_out(file, fh, fmt);
2218
2219 for (i = 0; i < NUM_FORMATS; i++)
2220 if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
2221 break;
2222 if (i == NUM_FORMATS) {
2223 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
2224 ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
2225 return -EINVAL;
2226 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03002227
Hans Verkuil96b8e142009-02-18 13:13:31 -03002228 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002229
2230 if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
2231 fh->buffers.active != ZORAN_FREE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002232 dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
2233 ZR_DEVNAME(zr));
2234 res = -EBUSY;
2235 goto sfmtv4l_unlock_and_return;
2236 }
2237 if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
2238 fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
2239 if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
2240 fmt->fmt.pix.width = BUZ_MAX_WIDTH;
2241
Trent Piepho1159b7f2009-03-10 23:28:16 -03002242 map_mode_raw(fh);
2243
2244 res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
2245 &zoran_formats[i]);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002246 if (res)
2247 goto sfmtv4l_unlock_and_return;
2248
Trent Piepho1159b7f2009-03-10 23:28:16 -03002249 /* tell the user the results/missing stuff */
Hans Verkuil96b8e142009-02-18 13:13:31 -03002250 fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
2251 fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
2252 fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
2253 if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
2254 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
2255 else
2256 fmt->fmt.pix.field = V4L2_FIELD_TOP;
2257
Hans Verkuil96b8e142009-02-18 13:13:31 -03002258sfmtv4l_unlock_and_return:
2259 mutex_unlock(&zr->resource_lock);
2260 return res;
2261}
2262
2263static int zoran_g_fbuf(struct file *file, void *__fh,
2264 struct v4l2_framebuffer *fb)
2265{
2266 struct zoran_fh *fh = __fh;
2267 struct zoran *zr = fh->zr;
2268
2269 memset(fb, 0, sizeof(*fb));
2270 mutex_lock(&zr->resource_lock);
Hans Verkuil7f6adea2009-02-19 17:31:17 -03002271 fb->base = zr->vbuf_base;
2272 fb->fmt.width = zr->vbuf_width;
2273 fb->fmt.height = zr->vbuf_height;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002274 if (zr->overlay_settings.format)
2275 fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
Hans Verkuil7f6adea2009-02-19 17:31:17 -03002276 fb->fmt.bytesperline = zr->vbuf_bytesperline;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002277 mutex_unlock(&zr->resource_lock);
2278 fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
2279 fb->fmt.field = V4L2_FIELD_INTERLACED;
2280 fb->flags = V4L2_FBUF_FLAG_OVERLAY;
2281 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2282
2283 return 0;
2284}
2285
2286static int zoran_s_fbuf(struct file *file, void *__fh,
2287 struct v4l2_framebuffer *fb)
2288{
2289 struct zoran_fh *fh = __fh;
2290 struct zoran *zr = fh->zr;
2291 int i, res = 0;
2292 __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
2293
2294 for (i = 0; i < NUM_FORMATS; i++)
2295 if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
2296 break;
2297 if (i == NUM_FORMATS) {
2298 dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
2299 ZR_DEVNAME(zr), fb->fmt.pixelformat,
2300 (char *)&printformat);
2301 return -EINVAL;
2302 }
2303
2304 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002305 res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
2306 fb->fmt.height, fb->fmt.bytesperline);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002307 mutex_unlock(&zr->resource_lock);
2308
2309 return res;
2310}
2311
2312static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
2313{
2314 struct zoran_fh *fh = __fh;
2315 struct zoran *zr = fh->zr;
2316 int res;
2317
2318 mutex_lock(&zr->resource_lock);
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002319 res = setup_overlay(fh, on);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002320 mutex_unlock(&zr->resource_lock);
2321
2322 return res;
2323}
2324
Hans Verkuil84c1b092009-02-18 17:15:46 -03002325static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
2326
Hans Verkuil96b8e142009-02-18 13:13:31 -03002327static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
2328{
2329 struct zoran_fh *fh = __fh;
2330 struct zoran *zr = fh->zr;
2331 int res = 0;
2332
2333 if (req->memory != V4L2_MEMORY_MMAP) {
Hans Verkuil84c1b092009-02-18 17:15:46 -03002334 dprintk(2,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002335 KERN_ERR
2336 "%s: only MEMORY_MMAP capture is supported, not %d\n",
2337 ZR_DEVNAME(zr), req->memory);
2338 return -EINVAL;
2339 }
2340
Hans Verkuil84c1b092009-02-18 17:15:46 -03002341 if (req->count == 0)
2342 return zoran_streamoff(file, fh, req->type);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002343
Hans Verkuil84c1b092009-02-18 17:15:46 -03002344 mutex_lock(&zr->resource_lock);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002345 if (fh->buffers.allocated) {
Hans Verkuil84c1b092009-02-18 17:15:46 -03002346 dprintk(2,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002347 KERN_ERR
Hans Verkuil7f37cc92009-02-18 17:00:37 -03002348 "%s: VIDIOC_REQBUFS - buffers already allocated\n",
Hans Verkuil96b8e142009-02-18 13:13:31 -03002349 ZR_DEVNAME(zr));
2350 res = -EBUSY;
2351 goto v4l2reqbuf_unlock_and_return;
2352 }
2353
2354 if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
Trent Piepho1159b7f2009-03-10 23:28:16 -03002355 req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002356 /* control user input */
2357 if (req->count < 2)
2358 req->count = 2;
2359 if (req->count > v4l_nbufs)
2360 req->count = v4l_nbufs;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002361
2362 /* The next mmap will map the V4L buffers */
2363 map_mode_raw(fh);
2364 fh->buffers.num_buffers = req->count;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002365
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002366 if (v4l_fbuffer_alloc(fh)) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002367 res = -ENOMEM;
2368 goto v4l2reqbuf_unlock_and_return;
2369 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03002370 } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002371 fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002372 /* we need to calculate size ourselves now */
2373 if (req->count < 4)
2374 req->count = 4;
2375 if (req->count > jpg_nbufs)
2376 req->count = jpg_nbufs;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002377
2378 /* The next mmap will map the MJPEG buffers */
2379 map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
2380 fh->buffers.num_buffers = req->count;
2381 fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002382
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002383 if (jpg_fbuffer_alloc(fh)) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002384 res = -ENOMEM;
2385 goto v4l2reqbuf_unlock_and_return;
2386 }
Hans Verkuil96b8e142009-02-18 13:13:31 -03002387 } else {
2388 dprintk(1,
2389 KERN_ERR
2390 "%s: VIDIOC_REQBUFS - unknown type %d\n",
2391 ZR_DEVNAME(zr), req->type);
2392 res = -EINVAL;
2393 goto v4l2reqbuf_unlock_and_return;
2394 }
2395v4l2reqbuf_unlock_and_return:
2396 mutex_unlock(&zr->resource_lock);
2397
2398 return res;
2399}
2400
2401static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2402{
2403 struct zoran_fh *fh = __fh;
2404 struct zoran *zr = fh->zr;
Trent Piepho9cfb6a32009-03-03 20:44:45 -03002405 int res;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002406
2407 mutex_lock(&zr->resource_lock);
Trent Piephoe276f7b2009-03-05 08:02:05 -03002408 res = zoran_v4l2_buffer_status(fh, buf, buf->index);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002409 mutex_unlock(&zr->resource_lock);
2410
2411 return res;
2412}
2413
2414static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2415{
2416 struct zoran_fh *fh = __fh;
2417 struct zoran *zr = fh->zr;
2418 int res = 0, codec_mode, buf_type;
2419
2420 mutex_lock(&zr->resource_lock);
2421
2422 switch (fh->map_mode) {
2423 case ZORAN_MAP_MODE_RAW:
2424 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2425 dprintk(1, KERN_ERR
2426 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2427 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2428 res = -EINVAL;
2429 goto qbuf_unlock_and_return;
2430 }
2431
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002432 res = zoran_v4l_queue_frame(fh, buf->index);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002433 if (res)
2434 goto qbuf_unlock_and_return;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002435 if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002436 zr36057_set_memgrab(zr, 1);
2437 break;
2438
2439 case ZORAN_MAP_MODE_JPG_REC:
2440 case ZORAN_MAP_MODE_JPG_PLAY:
2441 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
2442 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2443 codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
2444 } else {
2445 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2446 codec_mode = BUZ_MODE_MOTION_COMPRESS;
2447 }
2448
2449 if (buf->type != buf_type) {
2450 dprintk(1, KERN_ERR
2451 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2452 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2453 res = -EINVAL;
2454 goto qbuf_unlock_and_return;
2455 }
2456
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002457 res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002458 if (res != 0)
2459 goto qbuf_unlock_and_return;
2460 if (zr->codec_mode == BUZ_MODE_IDLE &&
Trent Piepho1159b7f2009-03-10 23:28:16 -03002461 fh->buffers.active == ZORAN_LOCKED)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002462 zr36057_enable_jpg(zr, codec_mode);
Trent Piepho1159b7f2009-03-10 23:28:16 -03002463
Hans Verkuil96b8e142009-02-18 13:13:31 -03002464 break;
2465
2466 default:
2467 dprintk(1, KERN_ERR
2468 "%s: VIDIOC_QBUF - unsupported type %d\n",
2469 ZR_DEVNAME(zr), buf->type);
2470 res = -EINVAL;
2471 break;
2472 }
2473qbuf_unlock_and_return:
2474 mutex_unlock(&zr->resource_lock);
2475
2476 return res;
2477}
2478
2479static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
2480{
2481 struct zoran_fh *fh = __fh;
2482 struct zoran *zr = fh->zr;
2483 int res = 0, buf_type, num = -1; /* compiler borks here (?) */
2484
2485 mutex_lock(&zr->resource_lock);
2486
2487 switch (fh->map_mode) {
2488 case ZORAN_MAP_MODE_RAW:
2489 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2490 dprintk(1, KERN_ERR
2491 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2492 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2493 res = -EINVAL;
2494 goto dqbuf_unlock_and_return;
2495 }
2496
2497 num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
2498 if (file->f_flags & O_NONBLOCK &&
2499 zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
2500 res = -EAGAIN;
2501 goto dqbuf_unlock_and_return;
2502 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002503 res = v4l_sync(fh, num);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002504 if (res)
2505 goto dqbuf_unlock_and_return;
2506 zr->v4l_sync_tail++;
Trent Piephoe276f7b2009-03-05 08:02:05 -03002507 res = zoran_v4l2_buffer_status(fh, buf, num);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002508 break;
2509
2510 case ZORAN_MAP_MODE_JPG_REC:
2511 case ZORAN_MAP_MODE_JPG_PLAY:
2512 {
2513 struct zoran_sync bs;
2514
2515 if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
2516 buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2517 else
2518 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2519
2520 if (buf->type != buf_type) {
2521 dprintk(1, KERN_ERR
2522 "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
2523 ZR_DEVNAME(zr), buf->type, fh->map_mode);
2524 res = -EINVAL;
2525 goto dqbuf_unlock_and_return;
2526 }
2527
2528 num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
2529
2530 if (file->f_flags & O_NONBLOCK &&
2531 zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
2532 res = -EAGAIN;
2533 goto dqbuf_unlock_and_return;
2534 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002535 res = jpg_sync(fh, &bs);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002536 if (res)
2537 goto dqbuf_unlock_and_return;
Trent Piephoe276f7b2009-03-05 08:02:05 -03002538 res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002539 break;
2540 }
2541
2542 default:
2543 dprintk(1, KERN_ERR
2544 "%s: VIDIOC_DQBUF - unsupported type %d\n",
2545 ZR_DEVNAME(zr), buf->type);
2546 res = -EINVAL;
2547 break;
2548 }
2549dqbuf_unlock_and_return:
2550 mutex_unlock(&zr->resource_lock);
2551
2552 return res;
2553}
2554
2555static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
2556{
2557 struct zoran_fh *fh = __fh;
2558 struct zoran *zr = fh->zr;
2559 int res = 0;
2560
2561 mutex_lock(&zr->resource_lock);
2562
2563 switch (fh->map_mode) {
2564 case ZORAN_MAP_MODE_RAW: /* raw capture */
2565 if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002566 fh->buffers.active != ZORAN_ACTIVE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002567 res = -EBUSY;
2568 goto strmon_unlock_and_return;
2569 }
2570
Trent Piepho1159b7f2009-03-10 23:28:16 -03002571 zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002572 zr->v4l_settings = fh->v4l_settings;
2573
2574 zr->v4l_sync_tail = zr->v4l_pend_tail;
2575 if (!zr->v4l_memgrab_active &&
2576 zr->v4l_pend_head != zr->v4l_pend_tail) {
2577 zr36057_set_memgrab(zr, 1);
2578 }
2579 break;
2580
2581 case ZORAN_MAP_MODE_JPG_REC:
2582 case ZORAN_MAP_MODE_JPG_PLAY:
2583 /* what is the codec mode right now? */
2584 if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
Trent Piepho1159b7f2009-03-10 23:28:16 -03002585 fh->buffers.active != ZORAN_ACTIVE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002586 res = -EBUSY;
2587 goto strmon_unlock_and_return;
2588 }
2589
Trent Piepho1159b7f2009-03-10 23:28:16 -03002590 zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002591
2592 if (zr->jpg_que_head != zr->jpg_que_tail) {
2593 /* Start the jpeg codec when the first frame is queued */
2594 jpeg_start(zr);
2595 }
2596 break;
2597
2598 default:
2599 dprintk(1,
2600 KERN_ERR
2601 "%s: VIDIOC_STREAMON - invalid map mode %d\n",
2602 ZR_DEVNAME(zr), fh->map_mode);
2603 res = -EINVAL;
2604 break;
2605 }
2606strmon_unlock_and_return:
2607 mutex_unlock(&zr->resource_lock);
2608
2609 return res;
2610}
2611
2612static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
2613{
2614 struct zoran_fh *fh = __fh;
2615 struct zoran *zr = fh->zr;
2616 int i, res = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002617 unsigned long flags;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002618
2619 mutex_lock(&zr->resource_lock);
2620
2621 switch (fh->map_mode) {
2622 case ZORAN_MAP_MODE_RAW: /* raw capture */
Trent Piepho1159b7f2009-03-10 23:28:16 -03002623 if (fh->buffers.active == ZORAN_FREE &&
Hans Verkuil96b8e142009-02-18 13:13:31 -03002624 zr->v4l_buffers.active != ZORAN_FREE) {
2625 res = -EPERM; /* stay off other's settings! */
2626 goto strmoff_unlock_and_return;
2627 }
2628 if (zr->v4l_buffers.active == ZORAN_FREE)
2629 goto strmoff_unlock_and_return;
2630
Trent Piepho1159b7f2009-03-10 23:28:16 -03002631 spin_lock_irqsave(&zr->spinlock, flags);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002632 /* unload capture */
2633 if (zr->v4l_memgrab_active) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002634
Hans Verkuil96b8e142009-02-18 13:13:31 -03002635 zr36057_set_memgrab(zr, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002636 }
2637
Trent Piepho1159b7f2009-03-10 23:28:16 -03002638 for (i = 0; i < fh->buffers.num_buffers; i++)
Hans Verkuil96b8e142009-02-18 13:13:31 -03002639 zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
Trent Piepho1159b7f2009-03-10 23:28:16 -03002640 fh->buffers = zr->v4l_buffers;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002641
Trent Piepho1159b7f2009-03-10 23:28:16 -03002642 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002643
2644 zr->v4l_grab_seq = 0;
2645 zr->v4l_pend_head = zr->v4l_pend_tail = 0;
2646 zr->v4l_sync_tail = 0;
2647
Trent Piepho1159b7f2009-03-10 23:28:16 -03002648 spin_unlock_irqrestore(&zr->spinlock, flags);
2649
Hans Verkuil96b8e142009-02-18 13:13:31 -03002650 break;
2651
2652 case ZORAN_MAP_MODE_JPG_REC:
2653 case ZORAN_MAP_MODE_JPG_PLAY:
Trent Piepho1159b7f2009-03-10 23:28:16 -03002654 if (fh->buffers.active == ZORAN_FREE &&
Hans Verkuil96b8e142009-02-18 13:13:31 -03002655 zr->jpg_buffers.active != ZORAN_FREE) {
2656 res = -EPERM; /* stay off other's settings! */
2657 goto strmoff_unlock_and_return;
2658 }
2659 if (zr->jpg_buffers.active == ZORAN_FREE)
2660 goto strmoff_unlock_and_return;
2661
Trent Piephoe5ee3f62009-03-10 23:28:31 -03002662 res = jpg_qbuf(fh, -1,
Hans Verkuil96b8e142009-02-18 13:13:31 -03002663 (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
2664 BUZ_MODE_MOTION_COMPRESS :
2665 BUZ_MODE_MOTION_DECOMPRESS);
2666 if (res)
2667 goto strmoff_unlock_and_return;
2668 break;
2669 default:
2670 dprintk(1, KERN_ERR
2671 "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
2672 ZR_DEVNAME(zr), fh->map_mode);
2673 res = -EINVAL;
2674 break;
2675 }
2676strmoff_unlock_and_return:
2677 mutex_unlock(&zr->resource_lock);
2678
2679 return res;
2680}
2681
2682static int zoran_queryctrl(struct file *file, void *__fh,
2683 struct v4l2_queryctrl *ctrl)
2684{
Hans Verkuil107063c2009-02-18 17:26:06 -03002685 struct zoran_fh *fh = __fh;
2686 struct zoran *zr = fh->zr;
2687
Hans Verkuil96b8e142009-02-18 13:13:31 -03002688 /* we only support hue/saturation/contrast/brightness */
2689 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2690 ctrl->id > V4L2_CID_HUE)
2691 return -EINVAL;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002692
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002693 decoder_call(zr, core, queryctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002694
2695 return 0;
2696}
2697
2698static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2699{
2700 struct zoran_fh *fh = __fh;
2701 struct zoran *zr = fh->zr;
2702
2703 /* we only support hue/saturation/contrast/brightness */
2704 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2705 ctrl->id > V4L2_CID_HUE)
2706 return -EINVAL;
2707
2708 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002709 decoder_call(zr, core, g_ctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002710 mutex_unlock(&zr->resource_lock);
2711
2712 return 0;
2713}
2714
2715static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
2716{
2717 struct zoran_fh *fh = __fh;
2718 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002719
2720 /* we only support hue/saturation/contrast/brightness */
2721 if (ctrl->id < V4L2_CID_BRIGHTNESS ||
2722 ctrl->id > V4L2_CID_HUE)
2723 return -EINVAL;
2724
Hans Verkuil96b8e142009-02-18 13:13:31 -03002725 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002726 decoder_call(zr, core, s_ctrl, ctrl);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002727 mutex_unlock(&zr->resource_lock);
2728
2729 return 0;
2730}
2731
2732static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
2733{
2734 struct zoran_fh *fh = __fh;
2735 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002736
2737 mutex_lock(&zr->resource_lock);
Hans Verkuil107063c2009-02-18 17:26:06 -03002738 *std = zr->norm;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002739 mutex_unlock(&zr->resource_lock);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002740 return 0;
2741}
2742
2743static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
2744{
2745 struct zoran_fh *fh = __fh;
2746 struct zoran *zr = fh->zr;
Hans Verkuil107063c2009-02-18 17:26:06 -03002747 int res = 0;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002748
2749 mutex_lock(&zr->resource_lock);
Hans Verkuil107063c2009-02-18 17:26:06 -03002750 res = zoran_set_norm(zr, *std);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002751 if (res)
2752 goto sstd_unlock_and_return;
2753
2754 res = wait_grab_pending(zr);
2755sstd_unlock_and_return:
2756 mutex_unlock(&zr->resource_lock);
2757 return res;
2758}
2759
2760static int zoran_enum_input(struct file *file, void *__fh,
2761 struct v4l2_input *inp)
2762{
2763 struct zoran_fh *fh = __fh;
2764 struct zoran *zr = fh->zr;
Hans Verkuil96b8e142009-02-18 13:13:31 -03002765
2766 if (inp->index < 0 || inp->index >= zr->card.inputs)
2767 return -EINVAL;
2768 else {
2769 int id = inp->index;
2770 memset(inp, 0, sizeof(*inp));
2771 inp->index = id;
2772 }
2773
2774 strncpy(inp->name, zr->card.input[inp->index].name,
2775 sizeof(inp->name) - 1);
2776 inp->type = V4L2_INPUT_TYPE_CAMERA;
2777 inp->std = V4L2_STD_ALL;
2778
2779 /* Get status of video decoder */
2780 mutex_lock(&zr->resource_lock);
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -03002781 decoder_call(zr, video, g_input_status, &inp->status);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002782 mutex_unlock(&zr->resource_lock);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002783 return 0;
2784}
2785
2786static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
2787{
2788 struct zoran_fh *fh = __fh;
2789 struct zoran *zr = fh->zr;
2790
2791 mutex_lock(&zr->resource_lock);
2792 *input = zr->input;
2793 mutex_unlock(&zr->resource_lock);
2794
2795 return 0;
2796}
2797
2798static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
2799{
2800 struct zoran_fh *fh = __fh;
2801 struct zoran *zr = fh->zr;
2802 int res;
2803
2804 mutex_lock(&zr->resource_lock);
2805 res = zoran_set_input(zr, input);
2806 if (res)
2807 goto sinput_unlock_and_return;
2808
2809 /* Make sure the changes come into effect */
2810 res = wait_grab_pending(zr);
2811sinput_unlock_and_return:
2812 mutex_unlock(&zr->resource_lock);
2813 return res;
2814}
2815
2816static int zoran_enum_output(struct file *file, void *__fh,
2817 struct v4l2_output *outp)
2818{
2819 if (outp->index != 0)
2820 return -EINVAL;
2821
2822 memset(outp, 0, sizeof(*outp));
2823 outp->index = 0;
2824 outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
2825 strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
2826
2827 return 0;
2828}
2829
2830static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
2831{
2832 *output = 0;
2833
2834 return 0;
2835}
2836
2837static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
2838{
2839 if (output != 0)
2840 return -EINVAL;
2841
2842 return 0;
2843}
2844
2845/* cropping (sub-frame capture) */
2846static int zoran_cropcap(struct file *file, void *__fh,
2847 struct v4l2_cropcap *cropcap)
2848{
2849 struct zoran_fh *fh = __fh;
2850 struct zoran *zr = fh->zr;
2851 int type = cropcap->type, res = 0;
2852
2853 memset(cropcap, 0, sizeof(*cropcap));
2854 cropcap->type = type;
2855
2856 mutex_lock(&zr->resource_lock);
2857
2858 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2859 (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2860 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2861 dprintk(1, KERN_ERR
2862 "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
2863 ZR_DEVNAME(zr));
2864 res = -EINVAL;
2865 goto cropcap_unlock_and_return;
2866 }
2867
2868 cropcap->bounds.top = cropcap->bounds.left = 0;
2869 cropcap->bounds.width = BUZ_MAX_WIDTH;
2870 cropcap->bounds.height = BUZ_MAX_HEIGHT;
2871 cropcap->defrect.top = cropcap->defrect.left = 0;
2872 cropcap->defrect.width = BUZ_MIN_WIDTH;
2873 cropcap->defrect.height = BUZ_MIN_HEIGHT;
2874cropcap_unlock_and_return:
2875 mutex_unlock(&zr->resource_lock);
2876 return res;
2877}
2878
2879static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2880{
2881 struct zoran_fh *fh = __fh;
2882 struct zoran *zr = fh->zr;
2883 int type = crop->type, res = 0;
2884
2885 memset(crop, 0, sizeof(*crop));
2886 crop->type = type;
2887
2888 mutex_lock(&zr->resource_lock);
2889
2890 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2891 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2892 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2893 dprintk(1,
2894 KERN_ERR
2895 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2896 ZR_DEVNAME(zr));
2897 res = -EINVAL;
2898 goto gcrop_unlock_and_return;
2899 }
2900
2901 crop->c.top = fh->jpg_settings.img_y;
2902 crop->c.left = fh->jpg_settings.img_x;
2903 crop->c.width = fh->jpg_settings.img_width;
2904 crop->c.height = fh->jpg_settings.img_height;
2905
2906gcrop_unlock_and_return:
2907 mutex_unlock(&zr->resource_lock);
2908
2909 return res;
2910}
2911
2912static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
2913{
2914 struct zoran_fh *fh = __fh;
2915 struct zoran *zr = fh->zr;
2916 int res = 0;
2917 struct zoran_jpg_settings settings;
2918
2919 settings = fh->jpg_settings;
2920
2921 mutex_lock(&zr->resource_lock);
2922
Trent Piepho1159b7f2009-03-10 23:28:16 -03002923 if (fh->buffers.allocated) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03002924 dprintk(1, KERN_ERR
2925 "%s: VIDIOC_S_CROP - cannot change settings while active\n",
2926 ZR_DEVNAME(zr));
2927 res = -EBUSY;
2928 goto scrop_unlock_and_return;
2929 }
2930
2931 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
2932 (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2933 fh->map_mode == ZORAN_MAP_MODE_RAW)) {
2934 dprintk(1, KERN_ERR
2935 "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
2936 ZR_DEVNAME(zr));
2937 res = -EINVAL;
2938 goto scrop_unlock_and_return;
2939 }
2940
2941 /* move into a form that we understand */
2942 settings.img_x = crop->c.left;
2943 settings.img_y = crop->c.top;
2944 settings.img_width = crop->c.width;
2945 settings.img_height = crop->c.height;
2946
2947 /* check validity */
Hans Verkuil0ba514d2009-02-18 17:11:17 -03002948 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03002949 if (res)
2950 goto scrop_unlock_and_return;
2951
2952 /* accept */
2953 fh->jpg_settings = settings;
2954
2955scrop_unlock_and_return:
2956 mutex_unlock(&zr->resource_lock);
2957 return res;
2958}
2959
2960static int zoran_g_jpegcomp(struct file *file, void *__fh,
2961 struct v4l2_jpegcompression *params)
2962{
2963 struct zoran_fh *fh = __fh;
2964 struct zoran *zr = fh->zr;
2965 memset(params, 0, sizeof(*params));
2966
2967 mutex_lock(&zr->resource_lock);
2968
2969 params->quality = fh->jpg_settings.jpg_comp.quality;
2970 params->APPn = fh->jpg_settings.jpg_comp.APPn;
2971 memcpy(params->APP_data,
2972 fh->jpg_settings.jpg_comp.APP_data,
2973 fh->jpg_settings.jpg_comp.APP_len);
2974 params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
2975 memcpy(params->COM_data,
2976 fh->jpg_settings.jpg_comp.COM_data,
2977 fh->jpg_settings.jpg_comp.COM_len);
2978 params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
2979 params->jpeg_markers =
2980 fh->jpg_settings.jpg_comp.jpeg_markers;
2981
2982 mutex_unlock(&zr->resource_lock);
2983
2984 return 0;
2985}
2986
2987static int zoran_s_jpegcomp(struct file *file, void *__fh,
2988 struct v4l2_jpegcompression *params)
2989{
2990 struct zoran_fh *fh = __fh;
2991 struct zoran *zr = fh->zr;
2992 int res = 0;
2993 struct zoran_jpg_settings settings;
2994
2995 settings = fh->jpg_settings;
2996
2997 settings.jpg_comp = *params;
2998
2999 mutex_lock(&zr->resource_lock);
3000
Trent Piepho1159b7f2009-03-10 23:28:16 -03003001 if (fh->buffers.active != ZORAN_FREE) {
Hans Verkuil96b8e142009-02-18 13:13:31 -03003002 dprintk(1, KERN_WARNING
3003 "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
3004 ZR_DEVNAME(zr));
3005 res = -EBUSY;
3006 goto sjpegc_unlock_and_return;
3007 }
3008
Hans Verkuil0ba514d2009-02-18 17:11:17 -03003009 res = zoran_check_jpg_settings(zr, &settings, 0);
Hans Verkuil96b8e142009-02-18 13:13:31 -03003010 if (res)
3011 goto sjpegc_unlock_and_return;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003012 if (!fh->buffers.allocated)
3013 fh->buffers.buffer_size =
3014 zoran_v4l2_calc_bufsize(&fh->jpg_settings);
Hans Verkuil96b8e142009-02-18 13:13:31 -03003015 fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
3016sjpegc_unlock_and_return:
3017 mutex_unlock(&zr->resource_lock);
3018
Hans Verkuil27f79522009-02-20 07:34:28 -03003019 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022static unsigned int
3023zoran_poll (struct file *file,
3024 poll_table *wait)
3025{
3026 struct zoran_fh *fh = file->private_data;
3027 struct zoran *zr = fh->zr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 int res = 0, frame;
Trent Piephoe42af832007-07-17 18:29:43 -03003029 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /* we should check whether buffers are ready to be synced on
3032 * (w/o waits - O_NONBLOCK) here
3033 * if ready for read (sync), return POLLIN|POLLRDNORM,
3034 * if ready for write (sync), return POLLOUT|POLLWRNORM,
3035 * if error, return POLLERR,
3036 * if no buffers queued or so, return POLLNVAL
3037 */
3038
Ingo Molnar384c3682006-03-22 03:54:16 -03003039 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 switch (fh->map_mode) {
3042 case ZORAN_MAP_MODE_RAW:
Trent Piephoe42af832007-07-17 18:29:43 -03003043 poll_wait(file, &zr->v4l_capq, wait);
3044 frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
3045
3046 spin_lock_irqsave(&zr->spinlock, flags);
3047 dprintk(3,
3048 KERN_DEBUG
3049 "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -03003050 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003051 "FAL"[fh->buffers.active], zr->v4l_sync_tail,
Trent Piephoe42af832007-07-17 18:29:43 -03003052 "UPMD"[zr->v4l_buffers.buffer[frame].state],
3053 zr->v4l_pend_tail, zr->v4l_pend_head);
3054 /* Process is the one capturing? */
Trent Piepho1159b7f2009-03-10 23:28:16 -03003055 if (fh->buffers.active != ZORAN_FREE &&
Trent Piephoe42af832007-07-17 18:29:43 -03003056 /* Buffer ready to DQBUF? */
3057 zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 res = POLLIN | POLLRDNORM;
Trent Piephoe42af832007-07-17 18:29:43 -03003059 spin_unlock_irqrestore(&zr->spinlock, flags);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 break;
3062
3063 case ZORAN_MAP_MODE_JPG_REC:
3064 case ZORAN_MAP_MODE_JPG_PLAY:
Trent Piephoe42af832007-07-17 18:29:43 -03003065 poll_wait(file, &zr->jpg_capq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
Trent Piephoe42af832007-07-17 18:29:43 -03003067
3068 spin_lock_irqsave(&zr->spinlock, flags);
3069 dprintk(3,
3070 KERN_DEBUG
3071 "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
Harvey Harrison7e28adb2008-04-08 23:20:00 -03003072 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003073 "FAL"[fh->buffers.active], zr->jpg_que_tail,
Trent Piephoe42af832007-07-17 18:29:43 -03003074 "UPMD"[zr->jpg_buffers.buffer[frame].state],
3075 zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003076 if (fh->buffers.active != ZORAN_FREE &&
Trent Piephoe42af832007-07-17 18:29:43 -03003077 zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
3079 res = POLLIN | POLLRDNORM;
3080 else
3081 res = POLLOUT | POLLWRNORM;
3082 }
Trent Piephoe42af832007-07-17 18:29:43 -03003083 spin_unlock_irqrestore(&zr->spinlock, flags);
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 break;
3086
3087 default:
3088 dprintk(1,
Trent Piephoe42af832007-07-17 18:29:43 -03003089 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003090 "%s: %s - internal error, unknown map_mode=%d\n",
3091 ZR_DEVNAME(zr), __func__, fh->map_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 res = POLLNVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094
Ingo Molnar384c3682006-03-22 03:54:16 -03003095 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097 return res;
3098}
3099
3100
3101/*
3102 * This maps the buffers to user space.
3103 *
3104 * Depending on the state of fh->map_mode
3105 * the V4L or the MJPEG buffers are mapped
3106 * per buffer or all together
3107 *
3108 * Note that we need to connect to some
3109 * unmap signal event to unmap the de-allocate
3110 * the buffer accordingly (zoran_vm_close())
3111 */
3112
3113static void
3114zoran_vm_open (struct vm_area_struct *vma)
3115{
3116 struct zoran_mapping *map = vma->vm_private_data;
3117
3118 map->count++;
3119}
3120
3121static void
3122zoran_vm_close (struct vm_area_struct *vma)
3123{
3124 struct zoran_mapping *map = vma->vm_private_data;
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003125 struct zoran_fh *fh = map->file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct zoran *zr = fh->zr;
3127 int i;
3128
Trent Piepho1159b7f2009-03-10 23:28:16 -03003129 if (--map->count > 0)
3130 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Trent Piepho1159b7f2009-03-10 23:28:16 -03003132 dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
3133 __func__, mode_name(fh->map_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Trent Piepho1159b7f2009-03-10 23:28:16 -03003135 for (i = 0; i < fh->buffers.num_buffers; i++) {
3136 if (fh->buffers.buffer[i].map == map)
3137 fh->buffers.buffer[i].map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003139 kfree(map);
3140
3141 /* Any buffers still mapped? */
3142 for (i = 0; i < fh->buffers.num_buffers; i++)
3143 if (fh->buffers.buffer[i].map)
3144 return;
3145
3146 dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
3147 __func__, mode_name(fh->map_mode));
3148
3149 mutex_lock(&zr->resource_lock);
3150
3151 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
3152 if (fh->buffers.active != ZORAN_FREE) {
3153 unsigned long flags;
3154
3155 spin_lock_irqsave(&zr->spinlock, flags);
3156 zr36057_set_memgrab(zr, 0);
3157 zr->v4l_buffers.allocated = 0;
3158 zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
3159 spin_unlock_irqrestore(&zr->spinlock, flags);
3160 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003161 v4l_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003162 } else {
3163 if (fh->buffers.active != ZORAN_FREE) {
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003164 jpg_qbuf(fh, -1, zr->codec_mode);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003165 zr->jpg_buffers.allocated = 0;
3166 zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
3167 }
Trent Piephoe5ee3f62009-03-10 23:28:31 -03003168 jpg_fbuffer_free(fh);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003169 }
3170
3171 mutex_unlock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
3174static struct vm_operations_struct zoran_vm_ops = {
3175 .open = zoran_vm_open,
3176 .close = zoran_vm_close,
3177};
3178
3179static int
3180zoran_mmap (struct file *file,
3181 struct vm_area_struct *vma)
3182{
3183 struct zoran_fh *fh = file->private_data;
3184 struct zoran *zr = fh->zr;
3185 unsigned long size = (vma->vm_end - vma->vm_start);
3186 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3187 int i, j;
3188 unsigned long page, start = vma->vm_start, todo, pos, fraglen;
3189 int first, last;
3190 struct zoran_mapping *map;
3191 int res = 0;
3192
3193 dprintk(3,
Trent Piephoc61402b2009-03-10 23:28:33 -03003194 KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
3195 ZR_DEVNAME(zr), __func__,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003196 mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
3199 !(vma->vm_flags & VM_WRITE)) {
3200 dprintk(1,
3201 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003202 "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
3203 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return -EINVAL;
3205 }
3206
Trent Piepho1159b7f2009-03-10 23:28:16 -03003207 mutex_lock(&zr->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Trent Piepho1159b7f2009-03-10 23:28:16 -03003209 if (!fh->buffers.allocated) {
3210 dprintk(1,
3211 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003212 "%s: %s(%s) - buffers not yet allocated\n",
3213 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
Trent Piepho1159b7f2009-03-10 23:28:16 -03003214 res = -ENOMEM;
3215 goto mmap_unlock_and_return;
3216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Trent Piepho1159b7f2009-03-10 23:28:16 -03003218 first = offset / fh->buffers.buffer_size;
3219 last = first - 1 + size / fh->buffers.buffer_size;
3220 if (offset % fh->buffers.buffer_size != 0 ||
3221 size % fh->buffers.buffer_size != 0 || first < 0 ||
3222 last < 0 || first >= fh->buffers.num_buffers ||
3223 last >= fh->buffers.buffer_size) {
3224 dprintk(1,
3225 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003226 "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
3227 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
Trent Piepho1159b7f2009-03-10 23:28:16 -03003228 fh->buffers.buffer_size,
3229 fh->buffers.num_buffers);
3230 res = -EINVAL;
3231 goto mmap_unlock_and_return;
3232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Trent Piepho1159b7f2009-03-10 23:28:16 -03003234 /* Check if any buffers are already mapped */
3235 for (i = first; i <= last; i++) {
3236 if (fh->buffers.buffer[i].map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 dprintk(1,
3238 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003239 "%s: %s(%s) - buffer %d already mapped\n",
3240 ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003241 res = -EBUSY;
3242 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Trent Piepho1159b7f2009-03-10 23:28:16 -03003246 /* map these buffers */
3247 map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
3248 if (!map) {
3249 res = -ENOMEM;
3250 goto mmap_unlock_and_return;
3251 }
3252 map->file = file;
3253 map->count = 1;
3254
3255 vma->vm_ops = &zoran_vm_ops;
3256 vma->vm_flags |= VM_DONTEXPAND;
3257 vma->vm_private_data = map;
3258
3259 if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 for (i = first; i <= last; i++) {
Trent Piepho1159b7f2009-03-10 23:28:16 -03003261 todo = size;
3262 if (todo > fh->buffers.buffer_size)
3263 todo = fh->buffers.buffer_size;
3264 page = fh->buffers.buffer[i].v4l.fbuffer_phys;
3265 if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
3266 todo, PAGE_SHARED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 dprintk(1,
3268 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003269 "%s: %s(V4L) - remap_pfn_range failed\n",
3270 ZR_DEVNAME(zr), __func__);
Trent Piepho1159b7f2009-03-10 23:28:16 -03003271 res = -EAGAIN;
3272 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003274 size -= todo;
3275 start += todo;
3276 fh->buffers.buffer[i].map = map;
3277 if (size == 0)
3278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003280 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 for (i = first; i <= last; i++) {
3282 for (j = 0;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003283 j < fh->buffers.buffer_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 j++) {
3285 fraglen =
Trent Piepho1159b7f2009-03-10 23:28:16 -03003286 (le32_to_cpu(fh->buffers.buffer[i].jpg.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 frag_tab[2 * j + 1]) & ~1) << 1;
3288 todo = size;
3289 if (todo > fraglen)
3290 todo = fraglen;
3291 pos =
Trent Piepho1159b7f2009-03-10 23:28:16 -03003292 le32_to_cpu(fh->buffers.
3293 buffer[i].jpg.frag_tab[2 * j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 /* should just be pos on i386 */
3295 page = virt_to_phys(bus_to_virt(pos))
3296 >> PAGE_SHIFT;
3297 if (remap_pfn_range(vma, start, page,
3298 todo, PAGE_SHARED)) {
3299 dprintk(1,
3300 KERN_ERR
Trent Piephoc61402b2009-03-10 23:28:33 -03003301 "%s: %s(V4L) - remap_pfn_range failed\n",
3302 ZR_DEVNAME(zr), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 res = -EAGAIN;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003304 goto mmap_unlock_and_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 size -= todo;
3307 start += todo;
3308 if (size == 0)
3309 break;
Trent Piepho1159b7f2009-03-10 23:28:16 -03003310 if (le32_to_cpu(fh->buffers.buffer[i].jpg.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 frag_tab[2 * j + 1]) & 1)
3312 break; /* was last fragment */
3313 }
Trent Piepho1159b7f2009-03-10 23:28:16 -03003314 fh->buffers.buffer[i].map = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (size == 0)
3316 break;
3317
3318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320
Trent Piepho1159b7f2009-03-10 23:28:16 -03003321mmap_unlock_and_return:
3322 mutex_unlock(&zr->resource_lock);
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return 0;
3325}
3326
Hans Verkuil96b8e142009-02-18 13:13:31 -03003327static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
3328 .vidioc_querycap = zoran_querycap,
3329 .vidioc_cropcap = zoran_cropcap,
3330 .vidioc_s_crop = zoran_s_crop,
3331 .vidioc_g_crop = zoran_g_crop,
3332 .vidioc_enum_input = zoran_enum_input,
3333 .vidioc_g_input = zoran_g_input,
3334 .vidioc_s_input = zoran_s_input,
3335 .vidioc_enum_output = zoran_enum_output,
3336 .vidioc_g_output = zoran_g_output,
3337 .vidioc_s_output = zoran_s_output,
3338 .vidioc_g_fbuf = zoran_g_fbuf,
3339 .vidioc_s_fbuf = zoran_s_fbuf,
3340 .vidioc_g_std = zoran_g_std,
3341 .vidioc_s_std = zoran_s_std,
3342 .vidioc_g_jpegcomp = zoran_g_jpegcomp,
3343 .vidioc_s_jpegcomp = zoran_s_jpegcomp,
3344 .vidioc_overlay = zoran_overlay,
3345 .vidioc_reqbufs = zoran_reqbufs,
3346 .vidioc_querybuf = zoran_querybuf,
3347 .vidioc_qbuf = zoran_qbuf,
3348 .vidioc_dqbuf = zoran_dqbuf,
3349 .vidioc_streamon = zoran_streamon,
3350 .vidioc_streamoff = zoran_streamoff,
3351 .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
3352 .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
3353 .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
3354 .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
3355 .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
3356 .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
3357 .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
3358 .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
3359 .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
3360 .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
3361 .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
3362 .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
3363 .vidioc_queryctrl = zoran_queryctrl,
3364 .vidioc_s_ctrl = zoran_s_ctrl,
3365 .vidioc_g_ctrl = zoran_g_ctrl,
Hans Verkuildcbd83b2009-02-18 13:51:13 -03003366#ifdef CONFIG_VIDEO_V4L1_COMPAT
Hans Verkuil96b8e142009-02-18 13:13:31 -03003367 .vidioc_default = zoran_default,
Hans Verkuildcbd83b2009-02-18 13:51:13 -03003368 .vidiocgmbuf = zoran_vidiocgmbuf,
3369#endif
Hans Verkuil96b8e142009-02-18 13:13:31 -03003370};
3371
Hans Verkuilbec43662008-12-30 06:58:20 -03003372static const struct v4l2_file_operations zoran_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .owner = THIS_MODULE,
3374 .open = zoran_open,
3375 .release = zoran_close,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003376 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 .read = zoran_read,
3378 .write = zoran_write,
3379 .mmap = zoran_mmap,
3380 .poll = zoran_poll,
3381};
3382
3383struct video_device zoran_template __devinitdata = {
3384 .name = ZORAN_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 .fops = &zoran_fops,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003386 .ioctl_ops = &zoran_ioctl_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 .release = &zoran_vdev_release,
Hans Verkuil96b8e142009-02-18 13:13:31 -03003388 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 .minor = -1
3390};
3391