Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
47 | #include <linux/init.h> |
48 | #include <linux/module.h> | |
49 | #include <linux/delay.h> | |
50 | #include <linux/slab.h> | |
51 | #include <linux/pci.h> | |
52 | #include <linux/vmalloc.h> | |
53 | #include <linux/wait.h> | |
1da177e4 LT |
54 | |
55 | #include <linux/interrupt.h> | |
56 | #include <linux/i2c.h> | |
57 | #include <linux/i2c-algo-bit.h> | |
58 | ||
59 | #include <linux/spinlock.h> | |
1da177e4 | 60 | |
debff5a7 | 61 | #include <linux/videodev2.h> |
5e87efa3 | 62 | #include <media/v4l2-common.h> |
35ea11ff | 63 | #include <media/v4l2-ioctl.h> |
e56c597b | 64 | #include <media/v4l2-event.h> |
1da177e4 LT |
65 | #include "videocodec.h" |
66 | ||
9a6ab769 | 67 | #include <asm/byteorder.h> |
1da177e4 LT |
68 | #include <asm/io.h> |
69 | #include <asm/uaccess.h> | |
70 | #include <linux/proc_fs.h> | |
71 | ||
384c3689 | 72 | #include <linux/mutex.h> |
1da177e4 LT |
73 | #include "zoran.h" |
74 | #include "zoran_device.h" | |
75 | #include "zoran_card.h" | |
76 | ||
603d6f2c TP |
77 | |
78 | const struct zoran_format zoran_formats[] = { | |
79 | { | |
80 | .name = "15-bit RGB LE", | |
602dd48a HV |
81 | .fourcc = V4L2_PIX_FMT_RGB555, |
82 | .colorspace = V4L2_COLORSPACE_SRGB, | |
1da177e4 LT |
83 | .depth = 15, |
84 | .flags = ZORAN_FORMAT_CAPTURE | | |
85 | ZORAN_FORMAT_OVERLAY, | |
603d6f2c TP |
86 | .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif| |
87 | ZR36057_VFESPFR_LittleEndian, | |
1da177e4 | 88 | }, { |
603d6f2c | 89 | .name = "15-bit RGB BE", |
602dd48a HV |
90 | .fourcc = V4L2_PIX_FMT_RGB555X, |
91 | .colorspace = V4L2_COLORSPACE_SRGB, | |
603d6f2c TP |
92 | .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", | |
602dd48a HV |
98 | .fourcc = V4L2_PIX_FMT_RGB565, |
99 | .colorspace = V4L2_COLORSPACE_SRGB, | |
1da177e4 LT |
100 | .depth = 16, |
101 | .flags = ZORAN_FORMAT_CAPTURE | | |
102 | ZORAN_FORMAT_OVERLAY, | |
603d6f2c TP |
103 | .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif| |
104 | ZR36057_VFESPFR_LittleEndian, | |
105 | }, { | |
106 | .name = "16-bit RGB BE", | |
602dd48a HV |
107 | .fourcc = V4L2_PIX_FMT_RGB565X, |
108 | .colorspace = V4L2_COLORSPACE_SRGB, | |
603d6f2c TP |
109 | .depth = 16, |
110 | .flags = ZORAN_FORMAT_CAPTURE | | |
111 | ZORAN_FORMAT_OVERLAY, | |
112 | .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif, | |
1da177e4 LT |
113 | }, { |
114 | .name = "24-bit RGB", | |
602dd48a HV |
115 | .fourcc = V4L2_PIX_FMT_BGR24, |
116 | .colorspace = V4L2_COLORSPACE_SRGB, | |
1da177e4 LT |
117 | .depth = 24, |
118 | .flags = ZORAN_FORMAT_CAPTURE | | |
119 | ZORAN_FORMAT_OVERLAY, | |
603d6f2c | 120 | .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24, |
1da177e4 | 121 | }, { |
603d6f2c | 122 | .name = "32-bit RGB LE", |
602dd48a HV |
123 | .fourcc = V4L2_PIX_FMT_BGR32, |
124 | .colorspace = V4L2_COLORSPACE_SRGB, | |
1da177e4 LT |
125 | .depth = 32, |
126 | .flags = ZORAN_FORMAT_CAPTURE | | |
127 | ZORAN_FORMAT_OVERLAY, | |
603d6f2c TP |
128 | .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian, |
129 | }, { | |
130 | .name = "32-bit RGB BE", | |
602dd48a HV |
131 | .fourcc = V4L2_PIX_FMT_RGB32, |
132 | .colorspace = V4L2_COLORSPACE_SRGB, | |
603d6f2c TP |
133 | .depth = 32, |
134 | .flags = ZORAN_FORMAT_CAPTURE | | |
135 | ZORAN_FORMAT_OVERLAY, | |
136 | .vfespfr = ZR36057_VFESPFR_RGB888, | |
1da177e4 LT |
137 | }, { |
138 | .name = "4:2:2, packed, YUYV", | |
602dd48a HV |
139 | .fourcc = V4L2_PIX_FMT_YUYV, |
140 | .colorspace = V4L2_COLORSPACE_SMPTE170M, | |
1da177e4 LT |
141 | .depth = 16, |
142 | .flags = ZORAN_FORMAT_CAPTURE | | |
d56410e0 | 143 | ZORAN_FORMAT_OVERLAY, |
603d6f2c TP |
144 | .vfespfr = ZR36057_VFESPFR_YUV422, |
145 | }, { | |
146 | .name = "4:2:2, packed, UYVY", | |
602dd48a HV |
147 | .fourcc = V4L2_PIX_FMT_UYVY, |
148 | .colorspace = V4L2_COLORSPACE_SMPTE170M, | |
603d6f2c TP |
149 | .depth = 16, |
150 | .flags = ZORAN_FORMAT_CAPTURE | | |
151 | ZORAN_FORMAT_OVERLAY, | |
152 | .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian, | |
1da177e4 LT |
153 | }, { |
154 | .name = "Hardware-encoded Motion-JPEG", | |
602dd48a HV |
155 | .fourcc = V4L2_PIX_FMT_MJPEG, |
156 | .colorspace = V4L2_COLORSPACE_SMPTE170M, | |
1da177e4 LT |
157 | .depth = 0, |
158 | .flags = ZORAN_FORMAT_CAPTURE | | |
159 | ZORAN_FORMAT_PLAYBACK | | |
160 | ZORAN_FORMAT_COMPRESSED, | |
161 | } | |
162 | }; | |
603d6f2c | 163 | #define NUM_FORMATS ARRAY_SIZE(zoran_formats) |
1da177e4 | 164 | |
1da177e4 LT |
165 | /* small helper function for calculating buffersizes for v4l2 |
166 | * we calculate the nearest higher power-of-two, which | |
167 | * will be the recommended buffersize */ | |
168 | static __u32 | |
169 | zoran_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 | } | |
1da177e4 LT |
187 | |
188 | /* forward references */ | |
e5ee3f64 TP |
189 | static void v4l_fbuffer_free(struct zoran_fh *fh); |
190 | static void jpg_fbuffer_free(struct zoran_fh *fh); | |
1da177e4 | 191 | |
1159b7f1 TP |
192 | /* Set mapping mode */ |
193 | static 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 | } | |
199 | static 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 | } | |
205 | static inline const char *mode_name(enum zoran_map_mode mode) | |
206 | { | |
207 | return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG"; | |
208 | } | |
209 | ||
1da177e4 LT |
210 | /* |
211 | * Allocate the V4L grab buffers | |
212 | * | |
213 | * These have to be pysically contiguous. | |
1da177e4 LT |
214 | */ |
215 | ||
e5ee3f64 | 216 | static int v4l_fbuffer_alloc(struct zoran_fh *fh) |
1da177e4 | 217 | { |
1da177e4 LT |
218 | struct zoran *zr = fh->zr; |
219 | int i, off; | |
220 | unsigned char *mem; | |
1da177e4 | 221 | |
1159b7f1 TP |
222 | for (i = 0; i < fh->buffers.num_buffers; i++) { |
223 | if (fh->buffers.buffer[i].v4l.fbuffer) | |
1da177e4 LT |
224 | dprintk(2, |
225 | KERN_WARNING | |
c61402ba TP |
226 | "%s: %s - buffer %d already allocated!?\n", |
227 | ZR_DEVNAME(zr), __func__, i); | |
1da177e4 LT |
228 | |
229 | //udelay(20); | |
ee9a9d66 JD |
230 | mem = kmalloc(fh->buffers.buffer_size, |
231 | GFP_KERNEL | __GFP_NOWARN); | |
b80696b7 HV |
232 | if (!mem) { |
233 | dprintk(1, | |
234 | KERN_ERR | |
c61402ba TP |
235 | "%s: %s - kmalloc for V4L buf %d failed\n", |
236 | ZR_DEVNAME(zr), __func__, i); | |
e5ee3f64 | 237 | v4l_fbuffer_free(fh); |
b80696b7 | 238 | return -ENOBUFS; |
1da177e4 | 239 | } |
1159b7f1 TP |
240 | 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; | |
b80696b7 | 244 | off += PAGE_SIZE) |
602dd48a | 245 | SetPageReserved(virt_to_page(mem + off)); |
b80696b7 HV |
246 | dprintk(4, |
247 | KERN_INFO | |
8737f66e | 248 | "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n", |
c61402ba | 249 | ZR_DEVNAME(zr), __func__, i, (unsigned long) mem, |
8737f66e | 250 | (unsigned long long)virt_to_bus(mem)); |
1da177e4 LT |
251 | } |
252 | ||
1159b7f1 | 253 | fh->buffers.allocated = 1; |
1da177e4 LT |
254 | |
255 | return 0; | |
256 | } | |
257 | ||
258 | /* free the V4L grab buffers */ | |
e5ee3f64 | 259 | static void v4l_fbuffer_free(struct zoran_fh *fh) |
1da177e4 | 260 | { |
1da177e4 LT |
261 | struct zoran *zr = fh->zr; |
262 | int i, off; | |
263 | unsigned char *mem; | |
264 | ||
c61402ba | 265 | dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__); |
1da177e4 | 266 | |
1159b7f1 TP |
267 | for (i = 0; i < fh->buffers.num_buffers; i++) { |
268 | if (!fh->buffers.buffer[i].v4l.fbuffer) | |
1da177e4 LT |
269 | continue; |
270 | ||
1159b7f1 TP |
271 | mem = fh->buffers.buffer[i].v4l.fbuffer; |
272 | for (off = 0; off < fh->buffers.buffer_size; | |
b80696b7 | 273 | off += PAGE_SIZE) |
602dd48a | 274 | ClearPageReserved(virt_to_page(mem + off)); |
1159b7f1 TP |
275 | kfree(fh->buffers.buffer[i].v4l.fbuffer); |
276 | fh->buffers.buffer[i].v4l.fbuffer = NULL; | |
1da177e4 LT |
277 | } |
278 | ||
1159b7f1 | 279 | fh->buffers.allocated = 0; |
1da177e4 LT |
280 | } |
281 | ||
282 | /* | |
283 | * Allocate the MJPEG grab buffers. | |
284 | * | |
1da177e4 LT |
285 | * 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) | |
1da177e4 LT |
289 | * |
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 | ||
e5ee3f64 | 310 | static int jpg_fbuffer_alloc(struct zoran_fh *fh) |
1da177e4 | 311 | { |
1da177e4 LT |
312 | struct zoran *zr = fh->zr; |
313 | int i, j, off; | |
1159b7f1 | 314 | u8 *mem; |
1da177e4 | 315 | |
1159b7f1 TP |
316 | for (i = 0; i < fh->buffers.num_buffers; i++) { |
317 | if (fh->buffers.buffer[i].jpg.frag_tab) | |
1da177e4 LT |
318 | dprintk(2, |
319 | KERN_WARNING | |
c61402ba TP |
320 | "%s: %s - buffer %d already allocated!?\n", |
321 | ZR_DEVNAME(zr), __func__, i); | |
1da177e4 LT |
322 | |
323 | /* Allocate fragment table for this buffer */ | |
324 | ||
1159b7f1 | 325 | mem = (void *)get_zeroed_page(GFP_KERNEL); |
cebedf15 | 326 | if (!mem) { |
1da177e4 LT |
327 | dprintk(1, |
328 | KERN_ERR | |
c61402ba TP |
329 | "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n", |
330 | ZR_DEVNAME(zr), __func__, i); | |
e5ee3f64 | 331 | jpg_fbuffer_free(fh); |
1da177e4 LT |
332 | return -ENOBUFS; |
333 | } | |
1159b7f1 TP |
334 | fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem; |
335 | fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem); | |
336 | ||
337 | if (fh->buffers.need_contiguous) { | |
338 | mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL); | |
339 | if (mem == NULL) { | |
1da177e4 LT |
340 | dprintk(1, |
341 | KERN_ERR | |
c61402ba TP |
342 | "%s: %s - kmalloc failed for buffer %d\n", |
343 | ZR_DEVNAME(zr), __func__, i); | |
e5ee3f64 | 344 | jpg_fbuffer_free(fh); |
1da177e4 LT |
345 | return -ENOBUFS; |
346 | } | |
1159b7f1 TP |
347 | 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) | |
602dd48a | 352 | SetPageReserved(virt_to_page(mem + off)); |
1da177e4 | 353 | } else { |
b80696b7 | 354 | /* jpg_bufsize is already page aligned */ |
1159b7f1 TP |
355 | for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) { |
356 | mem = (void *)get_zeroed_page(GFP_KERNEL); | |
357 | if (mem == NULL) { | |
1da177e4 LT |
358 | dprintk(1, |
359 | KERN_ERR | |
c61402ba TP |
360 | "%s: %s - get_zeroed_page failed for buffer %d\n", |
361 | ZR_DEVNAME(zr), __func__, i); | |
e5ee3f64 | 362 | jpg_fbuffer_free(fh); |
1da177e4 LT |
363 | return -ENOBUFS; |
364 | } | |
365 | ||
1159b7f1 TP |
366 | 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); | |
602dd48a | 370 | SetPageReserved(virt_to_page(mem)); |
1da177e4 LT |
371 | } |
372 | ||
1159b7f1 | 373 | fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1); |
1da177e4 LT |
374 | } |
375 | } | |
376 | ||
377 | dprintk(4, | |
c61402ba TP |
378 | KERN_DEBUG "%s: %s - %d KB allocated\n", |
379 | ZR_DEVNAME(zr), __func__, | |
1159b7f1 | 380 | (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10); |
1da177e4 | 381 | |
1159b7f1 | 382 | fh->buffers.allocated = 1; |
1da177e4 LT |
383 | |
384 | return 0; | |
385 | } | |
386 | ||
387 | /* free the MJPEG grab buffers */ | |
e5ee3f64 | 388 | static void jpg_fbuffer_free(struct zoran_fh *fh) |
1da177e4 | 389 | { |
1da177e4 LT |
390 | struct zoran *zr = fh->zr; |
391 | int i, j, off; | |
392 | unsigned char *mem; | |
602dd48a | 393 | __le32 frag_tab; |
1159b7f1 | 394 | struct zoran_buffer *buffer; |
1da177e4 | 395 | |
c61402ba | 396 | dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__); |
1da177e4 | 397 | |
1159b7f1 TP |
398 | for (i = 0, buffer = &fh->buffers.buffer[0]; |
399 | i < fh->buffers.num_buffers; i++, buffer++) { | |
400 | if (!buffer->jpg.frag_tab) | |
1da177e4 LT |
401 | continue; |
402 | ||
1159b7f1 TP |
403 | if (fh->buffers.need_contiguous) { |
404 | frag_tab = buffer->jpg.frag_tab[0]; | |
602dd48a HV |
405 | |
406 | if (frag_tab) { | |
1159b7f1 TP |
407 | mem = bus_to_virt(le32_to_cpu(frag_tab)); |
408 | for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE) | |
602dd48a | 409 | ClearPageReserved(virt_to_page(mem + off)); |
f9101210 | 410 | kfree(mem); |
1159b7f1 TP |
411 | buffer->jpg.frag_tab[0] = 0; |
412 | buffer->jpg.frag_tab[1] = 0; | |
1da177e4 LT |
413 | } |
414 | } else { | |
1159b7f1 TP |
415 | for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) { |
416 | frag_tab = buffer->jpg.frag_tab[2 * j]; | |
602dd48a HV |
417 | |
418 | if (!frag_tab) | |
1da177e4 | 419 | break; |
602dd48a HV |
420 | 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))); | |
1159b7f1 TP |
422 | buffer->jpg.frag_tab[2 * j] = 0; |
423 | buffer->jpg.frag_tab[2 * j + 1] = 0; | |
1da177e4 LT |
424 | } |
425 | } | |
426 | ||
1159b7f1 TP |
427 | free_page((unsigned long)buffer->jpg.frag_tab); |
428 | buffer->jpg.frag_tab = NULL; | |
1da177e4 LT |
429 | } |
430 | ||
1159b7f1 | 431 | fh->buffers.allocated = 0; |
1da177e4 LT |
432 | } |
433 | ||
434 | /* | |
435 | * V4L Buffer grabbing | |
436 | */ | |
437 | ||
438 | static int | |
1159b7f1 | 439 | zoran_v4l_set_format (struct zoran_fh *fh, |
1da177e4 LT |
440 | int width, |
441 | int height, | |
442 | const struct zoran_format *format) | |
443 | { | |
1da177e4 LT |
444 | 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 | |
c61402ba TP |
453 | "%s: %s - wrong frame size (%dx%d)\n", |
454 | ZR_DEVNAME(zr), __func__, width, height); | |
1da177e4 LT |
455 | return -EINVAL; |
456 | } | |
457 | ||
458 | bpp = (format->depth + 7) / 8; | |
459 | ||
460 | /* Check against available buffer size */ | |
1159b7f1 | 461 | if (height * width * bpp > fh->buffers.buffer_size) { |
1da177e4 LT |
462 | dprintk(1, |
463 | KERN_ERR | |
c61402ba TP |
464 | "%s: %s - video buffer size (%d kB) is too small\n", |
465 | ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10); | |
1da177e4 LT |
466 | 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 | |
c61402ba TP |
474 | "%s: %s - wrong frame alignment\n", |
475 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
476 | 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 | ||
e5ee3f64 | 487 | static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num) |
1da177e4 | 488 | { |
1da177e4 LT |
489 | struct zoran *zr = fh->zr; |
490 | unsigned long flags; | |
491 | int res = 0; | |
492 | ||
1159b7f1 | 493 | if (!fh->buffers.allocated) { |
1da177e4 LT |
494 | dprintk(1, |
495 | KERN_ERR | |
c61402ba TP |
496 | "%s: %s - buffers not yet allocated\n", |
497 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
498 | res = -ENOMEM; |
499 | } | |
500 | ||
501 | /* No grabbing outside the buffer range! */ | |
1159b7f1 | 502 | if (num >= fh->buffers.num_buffers || num < 0) { |
1da177e4 LT |
503 | dprintk(1, |
504 | KERN_ERR | |
c61402ba TP |
505 | "%s: %s - buffer %d is out of range\n", |
506 | ZR_DEVNAME(zr), __func__, num); | |
1da177e4 LT |
507 | res = -EINVAL; |
508 | } | |
509 | ||
510 | spin_lock_irqsave(&zr->spinlock, flags); | |
511 | ||
1159b7f1 | 512 | if (fh->buffers.active == ZORAN_FREE) { |
1da177e4 | 513 | if (zr->v4l_buffers.active == ZORAN_FREE) { |
1159b7f1 TP |
514 | zr->v4l_buffers = fh->buffers; |
515 | fh->buffers.active = ZORAN_ACTIVE; | |
1da177e4 LT |
516 | } else { |
517 | dprintk(1, | |
518 | KERN_ERR | |
c61402ba TP |
519 | "%s: %s - another session is already capturing\n", |
520 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
521 | 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) { | |
1159b7f1 | 531 | fh->buffers.active = ZORAN_FREE; |
1da177e4 LT |
532 | 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 | |
c61402ba TP |
539 | "%s: %s - queueing buffer %d in state DONE!?\n", |
540 | ZR_DEVNAME(zr), __func__, num); | |
1da177e4 LT |
541 | case BUZ_STATE_USER: |
542 | /* since there is at least one unused buffer there's room for at least | |
543 | * one more pend[] entry */ | |
1159b7f1 | 544 | zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num; |
1da177e4 LT |
545 | 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; | |
1159b7f1 | 549 | fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num]; |
1da177e4 LT |
550 | break; |
551 | } | |
552 | } | |
553 | ||
554 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
555 | ||
556 | if (!res && zr->v4l_buffers.active == ZORAN_FREE) | |
1159b7f1 | 557 | zr->v4l_buffers.active = fh->buffers.active; |
1da177e4 LT |
558 | |
559 | return res; | |
560 | } | |
561 | ||
1da177e4 LT |
562 | /* |
563 | * Sync on a V4L buffer | |
564 | */ | |
565 | ||
e5ee3f64 | 566 | static int v4l_sync(struct zoran_fh *fh, int frame) |
1da177e4 | 567 | { |
1da177e4 LT |
568 | struct zoran *zr = fh->zr; |
569 | unsigned long flags; | |
570 | ||
1159b7f1 | 571 | if (fh->buffers.active == ZORAN_FREE) { |
1da177e4 LT |
572 | dprintk(1, |
573 | KERN_ERR | |
c61402ba TP |
574 | "%s: %s - no grab active for this session\n", |
575 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
576 | return -EINVAL; |
577 | } | |
578 | ||
579 | /* check passed-in frame number */ | |
1159b7f1 | 580 | if (frame >= fh->buffers.num_buffers || frame < 0) { |
1da177e4 | 581 | dprintk(1, |
c61402ba TP |
582 | KERN_ERR "%s: %s - frame %d is invalid\n", |
583 | ZR_DEVNAME(zr), __func__, frame); | |
1da177e4 LT |
584 | 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 | |
c61402ba TP |
591 | "%s: %s - attempt to sync on a buffer which was not queued?\n", |
592 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
593 | return -EPROTO; |
594 | } | |
595 | ||
7b962d43 | 596 | mutex_unlock(&zr->lock); |
1da177e4 LT |
597 | /* wait on this buffer to get ready */ |
598 | if (!wait_event_interruptible_timeout(zr->v4l_capq, | |
7b962d43 HV |
599 | (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ)) { |
600 | mutex_lock(&zr->lock); | |
1da177e4 | 601 | return -ETIME; |
7b962d43 HV |
602 | } |
603 | mutex_lock(&zr->lock); | |
1da177e4 LT |
604 | if (signal_pending(current)) |
605 | return -ERESTARTSYS; | |
606 | ||
607 | /* buffer should now be in BUZ_STATE_DONE */ | |
608 | if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE) | |
609 | dprintk(2, | |
c61402ba TP |
610 | KERN_ERR "%s: %s - internal state error\n", |
611 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
612 | |
613 | zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER; | |
1159b7f1 | 614 | fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame]; |
1da177e4 LT |
615 | |
616 | spin_lock_irqsave(&zr->spinlock, flags); | |
617 | ||
618 | /* Check if streaming capture has finished */ | |
619 | if (zr->v4l_pend_tail == zr->v4l_pend_head) { | |
620 | zr36057_set_memgrab(zr, 0); | |
621 | if (zr->v4l_buffers.active == ZORAN_ACTIVE) { | |
1159b7f1 | 622 | fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE; |
1da177e4 LT |
623 | zr->v4l_buffers.allocated = 0; |
624 | } | |
625 | } | |
626 | ||
627 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Queue a MJPEG buffer for capture/playback | |
634 | */ | |
635 | ||
e5ee3f64 TP |
636 | static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num, |
637 | enum zoran_codec_mode mode) | |
1da177e4 | 638 | { |
1da177e4 LT |
639 | struct zoran *zr = fh->zr; |
640 | unsigned long flags; | |
641 | int res = 0; | |
642 | ||
643 | /* Check if buffers are allocated */ | |
1159b7f1 | 644 | if (!fh->buffers.allocated) { |
1da177e4 LT |
645 | dprintk(1, |
646 | KERN_ERR | |
c61402ba TP |
647 | "%s: %s - buffers not yet allocated\n", |
648 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
649 | return -ENOMEM; |
650 | } | |
651 | ||
652 | /* No grabbing outside the buffer range! */ | |
1159b7f1 | 653 | if (num >= fh->buffers.num_buffers || num < 0) { |
1da177e4 LT |
654 | dprintk(1, |
655 | KERN_ERR | |
c61402ba TP |
656 | "%s: %s - buffer %d out of range\n", |
657 | ZR_DEVNAME(zr), __func__, num); | |
1da177e4 LT |
658 | return -EINVAL; |
659 | } | |
660 | ||
661 | /* what is the codec mode right now? */ | |
662 | if (zr->codec_mode == BUZ_MODE_IDLE) { | |
663 | zr->jpg_settings = fh->jpg_settings; | |
664 | } else if (zr->codec_mode != mode) { | |
665 | /* wrong codec mode active - invalid */ | |
666 | dprintk(1, | |
667 | KERN_ERR | |
c61402ba TP |
668 | "%s: %s - codec in wrong mode\n", |
669 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
670 | return -EINVAL; |
671 | } | |
672 | ||
1159b7f1 | 673 | if (fh->buffers.active == ZORAN_FREE) { |
1da177e4 | 674 | if (zr->jpg_buffers.active == ZORAN_FREE) { |
1159b7f1 TP |
675 | zr->jpg_buffers = fh->buffers; |
676 | fh->buffers.active = ZORAN_ACTIVE; | |
1da177e4 LT |
677 | } else { |
678 | dprintk(1, | |
679 | KERN_ERR | |
c61402ba TP |
680 | "%s: %s - another session is already capturing\n", |
681 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
682 | res = -EBUSY; |
683 | } | |
684 | } | |
685 | ||
686 | if (!res && zr->codec_mode == BUZ_MODE_IDLE) { | |
687 | /* Ok load up the jpeg codec */ | |
688 | zr36057_enable_jpg(zr, mode); | |
689 | } | |
690 | ||
8bc3efcf RB |
691 | spin_lock_irqsave(&zr->spinlock, flags); |
692 | ||
1da177e4 LT |
693 | if (!res) { |
694 | switch (zr->jpg_buffers.buffer[num].state) { | |
695 | case BUZ_STATE_DONE: | |
696 | dprintk(2, | |
697 | KERN_WARNING | |
c61402ba TP |
698 | "%s: %s - queing frame in BUZ_STATE_DONE state!?\n", |
699 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
700 | case BUZ_STATE_USER: |
701 | /* since there is at least one unused buffer there's room for at | |
702 | *least one more pend[] entry */ | |
1159b7f1 | 703 | zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num; |
1da177e4 | 704 | zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND; |
1159b7f1 | 705 | fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num]; |
1da177e4 LT |
706 | zoran_feed_stat_com(zr); |
707 | break; | |
708 | default: | |
709 | case BUZ_STATE_DMA: | |
710 | case BUZ_STATE_PEND: | |
711 | if (zr->jpg_buffers.active == ZORAN_FREE) { | |
1159b7f1 | 712 | fh->buffers.active = ZORAN_FREE; |
1da177e4 LT |
713 | zr->jpg_buffers.allocated = 0; |
714 | } | |
715 | res = -EBUSY; /* what are you doing? */ | |
716 | break; | |
717 | } | |
718 | } | |
719 | ||
720 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
721 | ||
1159b7f1 TP |
722 | if (!res && zr->jpg_buffers.active == ZORAN_FREE) |
723 | zr->jpg_buffers.active = fh->buffers.active; | |
1da177e4 LT |
724 | |
725 | return res; | |
726 | } | |
727 | ||
e5ee3f64 | 728 | static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode) |
1da177e4 | 729 | { |
1da177e4 LT |
730 | struct zoran *zr = fh->zr; |
731 | int res = 0; | |
732 | ||
733 | /* Does the user want to stop streaming? */ | |
734 | if (frame < 0) { | |
735 | if (zr->codec_mode == mode) { | |
1159b7f1 | 736 | if (fh->buffers.active == ZORAN_FREE) { |
1da177e4 LT |
737 | dprintk(1, |
738 | KERN_ERR | |
c61402ba TP |
739 | "%s: %s(-1) - session not active\n", |
740 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
741 | return -EINVAL; |
742 | } | |
1159b7f1 | 743 | fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE; |
1da177e4 LT |
744 | zr->jpg_buffers.allocated = 0; |
745 | zr36057_enable_jpg(zr, BUZ_MODE_IDLE); | |
746 | return 0; | |
747 | } else { | |
748 | dprintk(1, | |
749 | KERN_ERR | |
c61402ba TP |
750 | "%s: %s - stop streaming but not in streaming mode\n", |
751 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
752 | return -EINVAL; |
753 | } | |
754 | } | |
755 | ||
e5ee3f64 | 756 | if ((res = zoran_jpg_queue_frame(fh, frame, mode))) |
1da177e4 LT |
757 | return res; |
758 | ||
759 | /* Start the jpeg codec when the first frame is queued */ | |
760 | if (!res && zr->jpg_que_head == 1) | |
761 | jpeg_start(zr); | |
762 | ||
763 | return res; | |
764 | } | |
765 | ||
766 | /* | |
767 | * Sync on a MJPEG buffer | |
768 | */ | |
769 | ||
e5ee3f64 | 770 | static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs) |
1da177e4 | 771 | { |
1da177e4 LT |
772 | struct zoran *zr = fh->zr; |
773 | unsigned long flags; | |
774 | int frame; | |
775 | ||
1159b7f1 | 776 | if (fh->buffers.active == ZORAN_FREE) { |
1da177e4 LT |
777 | dprintk(1, |
778 | KERN_ERR | |
c61402ba TP |
779 | "%s: %s - capture is not currently active\n", |
780 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
781 | return -EINVAL; |
782 | } | |
783 | if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS && | |
784 | zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) { | |
785 | dprintk(1, | |
786 | KERN_ERR | |
c61402ba TP |
787 | "%s: %s - codec not in streaming mode\n", |
788 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
789 | return -EINVAL; |
790 | } | |
7b962d43 | 791 | mutex_unlock(&zr->lock); |
1da177e4 LT |
792 | if (!wait_event_interruptible_timeout(zr->jpg_capq, |
793 | (zr->jpg_que_tail != zr->jpg_dma_tail || | |
794 | zr->jpg_dma_tail == zr->jpg_dma_head), | |
795 | 10*HZ)) { | |
796 | int isr; | |
797 | ||
798 | btand(~ZR36057_JMC_Go_en, ZR36057_JMC); | |
799 | udelay(1); | |
800 | zr->codec->control(zr->codec, CODEC_G_STATUS, | |
801 | sizeof(isr), &isr); | |
7b962d43 | 802 | mutex_lock(&zr->lock); |
1da177e4 LT |
803 | dprintk(1, |
804 | KERN_ERR | |
c61402ba TP |
805 | "%s: %s - timeout: codec isr=0x%02x\n", |
806 | ZR_DEVNAME(zr), __func__, isr); | |
1da177e4 LT |
807 | |
808 | return -ETIME; | |
809 | ||
810 | } | |
7b962d43 | 811 | mutex_lock(&zr->lock); |
1da177e4 LT |
812 | if (signal_pending(current)) |
813 | return -ERESTARTSYS; | |
814 | ||
815 | spin_lock_irqsave(&zr->spinlock, flags); | |
816 | ||
817 | if (zr->jpg_dma_tail != zr->jpg_dma_head) | |
818 | frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME]; | |
819 | else | |
820 | frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME]; | |
821 | ||
822 | /* buffer should now be in BUZ_STATE_DONE */ | |
18b548ca JD |
823 | if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE) |
824 | dprintk(2, | |
c61402ba TP |
825 | KERN_ERR "%s: %s - internal state error\n", |
826 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
827 | |
828 | *bs = zr->jpg_buffers.buffer[frame].bs; | |
829 | bs->frame = frame; | |
830 | zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER; | |
1159b7f1 | 831 | fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame]; |
1da177e4 LT |
832 | |
833 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
834 | ||
835 | return 0; | |
836 | } | |
837 | ||
e5ee3f64 | 838 | static void zoran_open_init_session(struct zoran_fh *fh) |
1da177e4 LT |
839 | { |
840 | int i; | |
1da177e4 LT |
841 | struct zoran *zr = fh->zr; |
842 | ||
843 | /* Per default, map the V4L Buffers */ | |
1159b7f1 | 844 | map_mode_raw(fh); |
1da177e4 LT |
845 | |
846 | /* take over the card's current settings */ | |
847 | fh->overlay_settings = zr->overlay_settings; | |
848 | fh->overlay_settings.is_set = 0; | |
849 | fh->overlay_settings.format = zr->overlay_settings.format; | |
850 | fh->overlay_active = ZORAN_FREE; | |
851 | ||
852 | /* v4l settings */ | |
853 | fh->v4l_settings = zr->v4l_settings; | |
1da177e4 LT |
854 | /* jpg settings */ |
855 | fh->jpg_settings = zr->jpg_settings; | |
856 | ||
1159b7f1 TP |
857 | /* buffers */ |
858 | memset(&fh->buffers, 0, sizeof(fh->buffers)); | |
859 | for (i = 0; i < MAX_FRAME; i++) { | |
860 | fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */ | |
861 | fh->buffers.buffer[i].bs.frame = i; | |
1da177e4 | 862 | } |
1159b7f1 TP |
863 | fh->buffers.allocated = 0; |
864 | fh->buffers.active = ZORAN_FREE; | |
1da177e4 LT |
865 | } |
866 | ||
e5ee3f64 | 867 | static void zoran_close_end_session(struct zoran_fh *fh) |
1da177e4 | 868 | { |
1da177e4 LT |
869 | struct zoran *zr = fh->zr; |
870 | ||
871 | /* overlay */ | |
872 | if (fh->overlay_active != ZORAN_FREE) { | |
873 | fh->overlay_active = zr->overlay_active = ZORAN_FREE; | |
874 | zr->v4l_overlay_active = 0; | |
875 | if (!zr->v4l_memgrab_active) | |
876 | zr36057_overlay(zr, 0); | |
877 | zr->overlay_mask = NULL; | |
878 | } | |
879 | ||
1159b7f1 TP |
880 | if (fh->map_mode == ZORAN_MAP_MODE_RAW) { |
881 | /* v4l capture */ | |
882 | if (fh->buffers.active != ZORAN_FREE) { | |
883 | unsigned long flags; | |
9896bbc1 | 884 | |
1159b7f1 TP |
885 | spin_lock_irqsave(&zr->spinlock, flags); |
886 | zr36057_set_memgrab(zr, 0); | |
887 | zr->v4l_buffers.allocated = 0; | |
888 | zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE; | |
889 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
890 | } | |
1da177e4 | 891 | |
1159b7f1 TP |
892 | /* v4l buffers */ |
893 | if (fh->buffers.allocated) | |
e5ee3f64 | 894 | v4l_fbuffer_free(fh); |
1159b7f1 TP |
895 | } else { |
896 | /* jpg capture */ | |
897 | if (fh->buffers.active != ZORAN_FREE) { | |
898 | zr36057_enable_jpg(zr, BUZ_MODE_IDLE); | |
899 | zr->jpg_buffers.allocated = 0; | |
900 | zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE; | |
901 | } | |
1da177e4 | 902 | |
1159b7f1 TP |
903 | /* jpg buffers */ |
904 | if (fh->buffers.allocated) | |
e5ee3f64 | 905 | jpg_fbuffer_free(fh); |
1da177e4 | 906 | } |
1da177e4 LT |
907 | } |
908 | ||
909 | /* | |
910 | * Open a zoran card. Right now the flags stuff is just playing | |
911 | */ | |
912 | ||
601139e0 | 913 | static int zoran_open(struct file *file) |
1da177e4 | 914 | { |
601139e0 | 915 | struct zoran *zr = video_drvdata(file); |
1da177e4 | 916 | struct zoran_fh *fh; |
fc96ab73 TP |
917 | int res, first_open = 0; |
918 | ||
c61402ba TP |
919 | dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n", |
920 | ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1); | |
1da177e4 | 921 | |
7b962d43 | 922 | mutex_lock(&zr->lock); |
1da177e4 | 923 | |
fc96ab73 TP |
924 | if (zr->user >= 2048) { |
925 | dprintk(1, KERN_ERR "%s: too many users (%d) on device\n", | |
926 | ZR_DEVNAME(zr), zr->user); | |
927 | res = -EBUSY; | |
928 | goto fail_unlock; | |
929 | } | |
930 | ||
1da177e4 | 931 | /* now, create the open()-specific file_ops struct */ |
7408187d | 932 | fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL); |
1da177e4 LT |
933 | if (!fh) { |
934 | dprintk(1, | |
935 | KERN_ERR | |
c61402ba TP |
936 | "%s: %s - allocation of zoran_fh failed\n", |
937 | ZR_DEVNAME(zr), __func__); | |
1da177e4 | 938 | res = -ENOMEM; |
0ab6e1c3 | 939 | goto fail_unlock; |
1da177e4 | 940 | } |
e56c597b HV |
941 | v4l2_fh_init(&fh->fh, video_devdata(file)); |
942 | ||
1da177e4 LT |
943 | /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows |
944 | * on norm-change! */ | |
945 | fh->overlay_mask = | |
946 | kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL); | |
947 | if (!fh->overlay_mask) { | |
948 | dprintk(1, | |
949 | KERN_ERR | |
c61402ba TP |
950 | "%s: %s - allocation of overlay_mask failed\n", |
951 | ZR_DEVNAME(zr), __func__); | |
1da177e4 | 952 | res = -ENOMEM; |
fc96ab73 | 953 | goto fail_fh; |
1da177e4 LT |
954 | } |
955 | ||
956 | if (zr->user++ == 0) | |
957 | first_open = 1; | |
958 | ||
1da177e4 LT |
959 | /* default setup - TODO: look at flags */ |
960 | if (first_open) { /* First device open */ | |
961 | zr36057_restart(zr); | |
962 | zoran_open_init_params(zr); | |
963 | zoran_init_hardware(zr); | |
964 | ||
965 | btor(ZR36057_ICR_IntPinEn, ZR36057_ICR); | |
966 | } | |
967 | ||
968 | /* set file_ops stuff */ | |
969 | file->private_data = fh; | |
970 | fh->zr = zr; | |
e5ee3f64 | 971 | zoran_open_init_session(fh); |
e56c597b | 972 | v4l2_fh_add(&fh->fh); |
7b962d43 | 973 | mutex_unlock(&zr->lock); |
1da177e4 LT |
974 | |
975 | return 0; | |
976 | ||
fc96ab73 TP |
977 | fail_fh: |
978 | kfree(fh); | |
fc96ab73 | 979 | fail_unlock: |
7b962d43 | 980 | mutex_unlock(&zr->lock); |
1da177e4 | 981 | |
fc96ab73 TP |
982 | dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n", |
983 | ZR_DEVNAME(zr), res, zr->user); | |
984 | ||
1da177e4 LT |
985 | return res; |
986 | } | |
987 | ||
988 | static int | |
bec43661 | 989 | zoran_close(struct file *file) |
1da177e4 LT |
990 | { |
991 | struct zoran_fh *fh = file->private_data; | |
992 | struct zoran *zr = fh->zr; | |
993 | ||
c61402ba TP |
994 | dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n", |
995 | ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1); | |
1da177e4 LT |
996 | |
997 | /* kernel locks (fs/device.c), so don't do that ourselves | |
998 | * (prevents deadlocks) */ | |
7b962d43 | 999 | mutex_lock(&zr->lock); |
1da177e4 | 1000 | |
e5ee3f64 | 1001 | zoran_close_end_session(fh); |
1da177e4 LT |
1002 | |
1003 | if (zr->user-- == 1) { /* Last process */ | |
1004 | /* Clean up JPEG process */ | |
1005 | wake_up_interruptible(&zr->jpg_capq); | |
1006 | zr36057_enable_jpg(zr, BUZ_MODE_IDLE); | |
1007 | zr->jpg_buffers.allocated = 0; | |
1008 | zr->jpg_buffers.active = ZORAN_FREE; | |
1009 | ||
1010 | /* disable interrupts */ | |
1011 | btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR); | |
1012 | ||
18b548ca | 1013 | if (zr36067_debug > 1) |
1da177e4 LT |
1014 | print_interrupts(zr); |
1015 | ||
1016 | /* Overlay off */ | |
1017 | zr->v4l_overlay_active = 0; | |
1018 | zr36057_overlay(zr, 0); | |
1019 | zr->overlay_mask = NULL; | |
1020 | ||
1021 | /* capture off */ | |
1022 | wake_up_interruptible(&zr->v4l_capq); | |
1023 | zr36057_set_memgrab(zr, 0); | |
1024 | zr->v4l_buffers.allocated = 0; | |
1025 | zr->v4l_buffers.active = ZORAN_FREE; | |
1026 | zoran_set_pci_master(zr, 0); | |
1027 | ||
1028 | if (!pass_through) { /* Switch to color bar */ | |
0ab6e1c3 | 1029 | decoder_call(zr, video, s_stream, 0); |
5325b427 | 1030 | encoder_call(zr, video, s_routing, 2, 0, 0); |
1da177e4 LT |
1031 | } |
1032 | } | |
7b962d43 | 1033 | mutex_unlock(&zr->lock); |
1da177e4 | 1034 | |
e56c597b HV |
1035 | v4l2_fh_del(&fh->fh); |
1036 | v4l2_fh_exit(&fh->fh); | |
1da177e4 LT |
1037 | kfree(fh->overlay_mask); |
1038 | kfree(fh); | |
1039 | ||
c61402ba | 1040 | dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__); |
1da177e4 LT |
1041 | |
1042 | return 0; | |
1043 | } | |
1044 | ||
e5ee3f64 | 1045 | static int setup_fbuffer(struct zoran_fh *fh, |
1da177e4 LT |
1046 | void *base, |
1047 | const struct zoran_format *fmt, | |
1048 | int width, | |
1049 | int height, | |
1050 | int bytesperline) | |
1051 | { | |
1da177e4 LT |
1052 | struct zoran *zr = fh->zr; |
1053 | ||
1054 | /* (Ronald) v4l/v4l2 guidelines */ | |
1055 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO)) | |
1056 | return -EPERM; | |
1057 | ||
e355880b AC |
1058 | /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on |
1059 | ALi Magik (that needs very low latency while the card needs a | |
1060 | higher value always) */ | |
1061 | ||
1062 | if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK)) | |
1063 | return -ENXIO; | |
1064 | ||
1da177e4 LT |
1065 | /* we need a bytesperline value, even if not given */ |
1066 | if (!bytesperline) | |
1067 | bytesperline = width * ((fmt->depth + 7) & ~7) / 8; | |
1068 | ||
1069 | #if 0 | |
1070 | if (zr->overlay_active) { | |
1071 | /* dzjee... stupid users... don't even bother to turn off | |
1072 | * overlay before changing the memory location... | |
1073 | * normally, we would return errors here. However, one of | |
1074 | * the tools that does this is... xawtv! and since xawtv | |
1075 | * is used by +/- 99% of the users, we'd rather be user- | |
1076 | * friendly and silently do as if nothing went wrong */ | |
1077 | dprintk(3, | |
1078 | KERN_ERR | |
c61402ba TP |
1079 | "%s: %s - forced overlay turnoff because framebuffer changed\n", |
1080 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1081 | zr36057_overlay(zr, 0); |
1082 | } | |
1083 | #endif | |
1084 | ||
1085 | if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) { | |
1086 | dprintk(1, | |
1087 | KERN_ERR | |
c61402ba TP |
1088 | "%s: %s - no valid overlay format given\n", |
1089 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1090 | return -EINVAL; |
1091 | } | |
1092 | if (height <= 0 || width <= 0 || bytesperline <= 0) { | |
1093 | dprintk(1, | |
1094 | KERN_ERR | |
c61402ba TP |
1095 | "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n", |
1096 | ZR_DEVNAME(zr), __func__, width, height, bytesperline); | |
1da177e4 LT |
1097 | return -EINVAL; |
1098 | } | |
1099 | if (bytesperline & 3) { | |
1100 | dprintk(1, | |
1101 | KERN_ERR | |
c61402ba TP |
1102 | "%s: %s - bytesperline (%d) must be 4-byte aligned\n", |
1103 | ZR_DEVNAME(zr), __func__, bytesperline); | |
1da177e4 LT |
1104 | return -EINVAL; |
1105 | } | |
1106 | ||
7f6adeaf HV |
1107 | zr->vbuf_base = (void *) ((unsigned long) base & ~3); |
1108 | zr->vbuf_height = height; | |
1109 | zr->vbuf_width = width; | |
1110 | zr->vbuf_depth = fmt->depth; | |
1da177e4 | 1111 | zr->overlay_settings.format = fmt; |
7f6adeaf | 1112 | zr->vbuf_bytesperline = bytesperline; |
1da177e4 LT |
1113 | |
1114 | /* The user should set new window parameters */ | |
1115 | zr->overlay_settings.is_set = 0; | |
1116 | ||
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | ||
32898a14 XW |
1121 | static int setup_window(struct zoran_fh *fh, |
1122 | int x, | |
1123 | int y, | |
1124 | int width, | |
1125 | int height, | |
1126 | struct v4l2_clip __user *clips, | |
1127 | unsigned int clipcount, | |
1128 | void __user *bitmap) | |
1da177e4 | 1129 | { |
1da177e4 | 1130 | struct zoran *zr = fh->zr; |
7f6adeaf | 1131 | struct v4l2_clip *vcp = NULL; |
1da177e4 LT |
1132 | int on, end; |
1133 | ||
1134 | ||
7f6adeaf | 1135 | if (!zr->vbuf_base) { |
1da177e4 LT |
1136 | dprintk(1, |
1137 | KERN_ERR | |
c61402ba TP |
1138 | "%s: %s - frame buffer has to be set first\n", |
1139 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1140 | return -EINVAL; |
1141 | } | |
1142 | ||
1143 | if (!fh->overlay_settings.format) { | |
1144 | dprintk(1, | |
1145 | KERN_ERR | |
c61402ba TP |
1146 | "%s: %s - no overlay format set\n", |
1147 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1148 | return -EINVAL; |
1149 | } | |
1150 | ||
32898a14 XW |
1151 | if (clipcount > 2048) { |
1152 | dprintk(1, | |
1153 | KERN_ERR | |
1154 | "%s: %s - invalid clipcount\n", | |
1155 | ZR_DEVNAME(zr), __func__); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | ||
1da177e4 LT |
1159 | /* |
1160 | * The video front end needs 4-byte alinged line sizes, we correct that | |
1161 | * silently here if necessary | |
1162 | */ | |
7f6adeaf | 1163 | if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) { |
1da177e4 LT |
1164 | end = (x + width) & ~1; /* round down */ |
1165 | x = (x + 1) & ~1; /* round up */ | |
1166 | width = end - x; | |
1167 | } | |
1168 | ||
7f6adeaf | 1169 | if (zr->vbuf_depth == 24) { |
1da177e4 LT |
1170 | end = (x + width) & ~3; /* round down */ |
1171 | x = (x + 3) & ~3; /* round up */ | |
1172 | width = end - x; | |
1173 | } | |
1174 | ||
1175 | if (width > BUZ_MAX_WIDTH) | |
1176 | width = BUZ_MAX_WIDTH; | |
1177 | if (height > BUZ_MAX_HEIGHT) | |
1178 | height = BUZ_MAX_HEIGHT; | |
1179 | ||
817f2c84 | 1180 | /* Check for invalid parameters */ |
1da177e4 LT |
1181 | if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT || |
1182 | width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) { | |
1183 | dprintk(1, | |
1184 | KERN_ERR | |
c61402ba TP |
1185 | "%s: %s - width = %d or height = %d invalid\n", |
1186 | ZR_DEVNAME(zr), __func__, width, height); | |
1da177e4 LT |
1187 | return -EINVAL; |
1188 | } | |
1189 | ||
1190 | fh->overlay_settings.x = x; | |
1191 | fh->overlay_settings.y = y; | |
1192 | fh->overlay_settings.width = width; | |
1193 | fh->overlay_settings.height = height; | |
1194 | fh->overlay_settings.clipcount = clipcount; | |
1195 | ||
1196 | /* | |
1197 | * If an overlay is running, we have to switch it off | |
1198 | * and switch it on again in order to get the new settings in effect. | |
1199 | * | |
1200 | * We also want to avoid that the overlay mask is written | |
1201 | * when an overlay is running. | |
1202 | */ | |
1203 | ||
1204 | on = zr->v4l_overlay_active && !zr->v4l_memgrab_active && | |
1205 | zr->overlay_active != ZORAN_FREE && | |
1206 | fh->overlay_active != ZORAN_FREE; | |
1207 | if (on) | |
1208 | zr36057_overlay(zr, 0); | |
1209 | ||
1210 | /* | |
1211 | * Write the overlay mask if clips are wanted. | |
1212 | * We prefer a bitmap. | |
1213 | */ | |
1214 | if (bitmap) { | |
1215 | /* fake value - it just means we want clips */ | |
1216 | fh->overlay_settings.clipcount = 1; | |
1217 | ||
1218 | if (copy_from_user(fh->overlay_mask, bitmap, | |
1219 | (width * height + 7) / 8)) { | |
1220 | return -EFAULT; | |
1221 | } | |
32898a14 | 1222 | } else if (clipcount) { |
1da177e4 | 1223 | /* write our own bitmap from the clips */ |
7f6adeaf | 1224 | vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4)); |
1da177e4 LT |
1225 | if (vcp == NULL) { |
1226 | dprintk(1, | |
1227 | KERN_ERR | |
c61402ba TP |
1228 | "%s: %s - Alloc of clip mask failed\n", |
1229 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1230 | return -ENOMEM; |
1231 | } | |
1232 | if (copy_from_user | |
7f6adeaf | 1233 | (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) { |
1da177e4 LT |
1234 | vfree(vcp); |
1235 | return -EFAULT; | |
1236 | } | |
e5ee3f64 | 1237 | write_overlay_mask(fh, vcp, clipcount); |
1da177e4 LT |
1238 | vfree(vcp); |
1239 | } | |
1240 | ||
1241 | fh->overlay_settings.is_set = 1; | |
1242 | if (fh->overlay_active != ZORAN_FREE && | |
1243 | zr->overlay_active != ZORAN_FREE) | |
1244 | zr->overlay_settings = fh->overlay_settings; | |
1245 | ||
1246 | if (on) | |
1247 | zr36057_overlay(zr, 1); | |
1248 | ||
1249 | /* Make sure the changes come into effect */ | |
1250 | return wait_grab_pending(zr); | |
1251 | } | |
1252 | ||
e5ee3f64 | 1253 | static int setup_overlay(struct zoran_fh *fh, int on) |
1da177e4 | 1254 | { |
1da177e4 LT |
1255 | struct zoran *zr = fh->zr; |
1256 | ||
25985edc | 1257 | /* If there is nothing to do, return immediately */ |
1da177e4 LT |
1258 | if ((on && fh->overlay_active != ZORAN_FREE) || |
1259 | (!on && fh->overlay_active == ZORAN_FREE)) | |
1260 | return 0; | |
1261 | ||
1262 | /* check whether we're touching someone else's overlay */ | |
1263 | if (on && zr->overlay_active != ZORAN_FREE && | |
1264 | fh->overlay_active == ZORAN_FREE) { | |
1265 | dprintk(1, | |
1266 | KERN_ERR | |
c61402ba TP |
1267 | "%s: %s - overlay is already active for another session\n", |
1268 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1269 | return -EBUSY; |
1270 | } | |
1271 | if (!on && zr->overlay_active != ZORAN_FREE && | |
1272 | fh->overlay_active == ZORAN_FREE) { | |
1273 | dprintk(1, | |
1274 | KERN_ERR | |
c61402ba TP |
1275 | "%s: %s - you cannot cancel someone else's session\n", |
1276 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1277 | return -EPERM; |
1278 | } | |
1279 | ||
1280 | if (on == 0) { | |
1281 | zr->overlay_active = fh->overlay_active = ZORAN_FREE; | |
1282 | zr->v4l_overlay_active = 0; | |
1283 | /* When a grab is running, the video simply | |
1284 | * won't be switched on any more */ | |
1285 | if (!zr->v4l_memgrab_active) | |
1286 | zr36057_overlay(zr, 0); | |
1287 | zr->overlay_mask = NULL; | |
1288 | } else { | |
7f6adeaf | 1289 | if (!zr->vbuf_base || !fh->overlay_settings.is_set) { |
1da177e4 LT |
1290 | dprintk(1, |
1291 | KERN_ERR | |
c61402ba TP |
1292 | "%s: %s - buffer or window not set\n", |
1293 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1294 | return -EINVAL; |
1295 | } | |
1296 | if (!fh->overlay_settings.format) { | |
1297 | dprintk(1, | |
1298 | KERN_ERR | |
c61402ba TP |
1299 | "%s: %s - no overlay format set\n", |
1300 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1301 | return -EINVAL; |
1302 | } | |
1303 | zr->overlay_active = fh->overlay_active = ZORAN_LOCKED; | |
1304 | zr->v4l_overlay_active = 1; | |
1305 | zr->overlay_mask = fh->overlay_mask; | |
1306 | zr->overlay_settings = fh->overlay_settings; | |
1307 | if (!zr->v4l_memgrab_active) | |
1308 | zr36057_overlay(zr, 1); | |
1309 | /* When a grab is running, the video will be | |
1310 | * switched on when grab is finished */ | |
1311 | } | |
1312 | ||
1313 | /* Make sure the changes come into effect */ | |
1314 | return wait_grab_pending(zr); | |
1315 | } | |
1316 | ||
e5ee3f64 TP |
1317 | /* get the status of a buffer in the clients buffer queue */ |
1318 | static int zoran_v4l2_buffer_status(struct zoran_fh *fh, | |
1319 | struct v4l2_buffer *buf, int num) | |
1da177e4 | 1320 | { |
1da177e4 | 1321 | struct zoran *zr = fh->zr; |
1159b7f1 | 1322 | unsigned long flags; |
1da177e4 | 1323 | |
1b18e7a0 | 1324 | buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
1da177e4 LT |
1325 | |
1326 | switch (fh->map_mode) { | |
1327 | case ZORAN_MAP_MODE_RAW: | |
1da177e4 | 1328 | /* check range */ |
1159b7f1 TP |
1329 | if (num < 0 || num >= fh->buffers.num_buffers || |
1330 | !fh->buffers.allocated) { | |
1da177e4 LT |
1331 | dprintk(1, |
1332 | KERN_ERR | |
c61402ba TP |
1333 | "%s: %s - wrong number or buffers not allocated\n", |
1334 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1335 | return -EINVAL; |
1336 | } | |
1337 | ||
1159b7f1 TP |
1338 | spin_lock_irqsave(&zr->spinlock, flags); |
1339 | dprintk(3, | |
1340 | KERN_DEBUG | |
1341 | "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n", | |
1342 | ZR_DEVNAME(zr), __func__, | |
1343 | "FAL"[fh->buffers.active], num, | |
1344 | "UPMD"[zr->v4l_buffers.buffer[num].state], | |
1345 | fh->buffers.buffer[num].map ? 'Y' : 'N'); | |
1346 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
1347 | ||
1da177e4 | 1348 | buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1159b7f1 | 1349 | buf->length = fh->buffers.buffer_size; |
1da177e4 LT |
1350 | |
1351 | /* get buffer */ | |
1159b7f1 TP |
1352 | buf->bytesused = fh->buffers.buffer[num].bs.length; |
1353 | if (fh->buffers.buffer[num].state == BUZ_STATE_DONE || | |
1354 | fh->buffers.buffer[num].state == BUZ_STATE_USER) { | |
1355 | buf->sequence = fh->buffers.buffer[num].bs.seq; | |
1da177e4 | 1356 | buf->flags |= V4L2_BUF_FLAG_DONE; |
1159b7f1 | 1357 | buf->timestamp = fh->buffers.buffer[num].bs.timestamp; |
1da177e4 LT |
1358 | } else { |
1359 | buf->flags |= V4L2_BUF_FLAG_QUEUED; | |
1360 | } | |
1361 | ||
1362 | if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2) | |
1363 | buf->field = V4L2_FIELD_TOP; | |
1364 | else | |
1365 | buf->field = V4L2_FIELD_INTERLACED; | |
1366 | ||
1367 | break; | |
1368 | ||
1369 | case ZORAN_MAP_MODE_JPG_REC: | |
1370 | case ZORAN_MAP_MODE_JPG_PLAY: | |
1371 | ||
1372 | /* check range */ | |
1159b7f1 TP |
1373 | if (num < 0 || num >= fh->buffers.num_buffers || |
1374 | !fh->buffers.allocated) { | |
1da177e4 LT |
1375 | dprintk(1, |
1376 | KERN_ERR | |
c61402ba TP |
1377 | "%s: %s - wrong number or buffers not allocated\n", |
1378 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1379 | return -EINVAL; |
1380 | } | |
1381 | ||
1382 | buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ? | |
1383 | V4L2_BUF_TYPE_VIDEO_CAPTURE : | |
1384 | V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
1159b7f1 | 1385 | buf->length = fh->buffers.buffer_size; |
1da177e4 LT |
1386 | |
1387 | /* these variables are only written after frame has been captured */ | |
1159b7f1 TP |
1388 | if (fh->buffers.buffer[num].state == BUZ_STATE_DONE || |
1389 | fh->buffers.buffer[num].state == BUZ_STATE_USER) { | |
1390 | buf->sequence = fh->buffers.buffer[num].bs.seq; | |
1391 | buf->timestamp = fh->buffers.buffer[num].bs.timestamp; | |
1392 | buf->bytesused = fh->buffers.buffer[num].bs.length; | |
1da177e4 LT |
1393 | buf->flags |= V4L2_BUF_FLAG_DONE; |
1394 | } else { | |
1395 | buf->flags |= V4L2_BUF_FLAG_QUEUED; | |
1396 | } | |
1397 | ||
1398 | /* which fields are these? */ | |
1399 | if (fh->jpg_settings.TmpDcm != 1) | |
1159b7f1 TP |
1400 | buf->field = fh->jpg_settings.odd_even ? |
1401 | V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM; | |
1da177e4 | 1402 | else |
1159b7f1 TP |
1403 | buf->field = fh->jpg_settings.odd_even ? |
1404 | V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT; | |
1da177e4 LT |
1405 | |
1406 | break; | |
1407 | ||
1408 | default: | |
1409 | ||
1410 | dprintk(5, | |
1411 | KERN_ERR | |
c61402ba TP |
1412 | "%s: %s - invalid buffer type|map_mode (%d|%d)\n", |
1413 | ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode); | |
1da177e4 LT |
1414 | return -EINVAL; |
1415 | } | |
1416 | ||
1417 | buf->memory = V4L2_MEMORY_MMAP; | |
1418 | buf->index = num; | |
1419 | buf->m.offset = buf->length * num; | |
1420 | ||
1421 | return 0; | |
1422 | } | |
1da177e4 LT |
1423 | |
1424 | static int | |
1425 | zoran_set_norm (struct zoran *zr, | |
107063c6 | 1426 | v4l2_std_id norm) |
1da177e4 | 1427 | { |
107063c6 | 1428 | int on; |
1da177e4 LT |
1429 | |
1430 | if (zr->v4l_buffers.active != ZORAN_FREE || | |
1431 | zr->jpg_buffers.active != ZORAN_FREE) { | |
1432 | dprintk(1, | |
1433 | KERN_WARNING | |
c61402ba TP |
1434 | "%s: %s called while in playback/capture mode\n", |
1435 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1436 | return -EBUSY; |
1437 | } | |
1438 | ||
107063c6 | 1439 | if (!(norm & zr->card.norms)) { |
1da177e4 | 1440 | dprintk(1, |
c61402ba TP |
1441 | KERN_ERR "%s: %s - unsupported norm %llx\n", |
1442 | ZR_DEVNAME(zr), __func__, norm); | |
1da177e4 LT |
1443 | return -EINVAL; |
1444 | } | |
1445 | ||
107063c6 HV |
1446 | if (norm & V4L2_STD_SECAM) |
1447 | zr->timing = zr->card.tvn[2]; | |
1448 | else if (norm & V4L2_STD_NTSC) | |
1449 | zr->timing = zr->card.tvn[1]; | |
1450 | else | |
1451 | zr->timing = zr->card.tvn[0]; | |
1da177e4 LT |
1452 | |
1453 | /* We switch overlay off and on since a change in the | |
1454 | * norm needs different VFE settings */ | |
1455 | on = zr->overlay_active && !zr->v4l_memgrab_active; | |
1456 | if (on) | |
1457 | zr36057_overlay(zr, 0); | |
1458 | ||
8774bed9 | 1459 | decoder_call(zr, video, s_std, norm); |
0ab6e1c3 | 1460 | encoder_call(zr, video, s_std_output, norm); |
1da177e4 LT |
1461 | |
1462 | if (on) | |
1463 | zr36057_overlay(zr, 1); | |
1464 | ||
1465 | /* Make sure the changes come into effect */ | |
1466 | zr->norm = norm; | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | static int | |
1472 | zoran_set_input (struct zoran *zr, | |
1473 | int input) | |
1474 | { | |
1da177e4 LT |
1475 | if (input == zr->input) { |
1476 | return 0; | |
1477 | } | |
1478 | ||
1479 | if (zr->v4l_buffers.active != ZORAN_FREE || | |
1480 | zr->jpg_buffers.active != ZORAN_FREE) { | |
1481 | dprintk(1, | |
1482 | KERN_WARNING | |
c61402ba TP |
1483 | "%s: %s called while in playback/capture mode\n", |
1484 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
1485 | return -EBUSY; |
1486 | } | |
1487 | ||
1488 | if (input < 0 || input >= zr->card.inputs) { | |
1489 | dprintk(1, | |
1490 | KERN_ERR | |
c61402ba TP |
1491 | "%s: %s - unnsupported input %d\n", |
1492 | ZR_DEVNAME(zr), __func__, input); | |
1da177e4 LT |
1493 | return -EINVAL; |
1494 | } | |
1495 | ||
1da177e4 LT |
1496 | zr->input = input; |
1497 | ||
5325b427 HV |
1498 | decoder_call(zr, video, s_routing, |
1499 | zr->card.input[input].muxsel, 0, 0); | |
1da177e4 LT |
1500 | |
1501 | return 0; | |
1502 | } | |
1503 | ||
1504 | /* | |
1505 | * ioctl routine | |
1506 | */ | |
1507 | ||
96b8e145 HV |
1508 | static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap) |
1509 | { | |
1510 | struct zoran_fh *fh = __fh; | |
1511 | struct zoran *zr = fh->zr; | |
1da177e4 | 1512 | |
96b8e145 HV |
1513 | strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1); |
1514 | strncpy(cap->driver, "zoran", sizeof(cap->driver)-1); | |
1515 | snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", | |
1516 | pci_name(zr->pci_dev)); | |
2eb3b1ad HV |
1517 | cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE | |
1518 | V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY; | |
1519 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
96b8e145 HV |
1520 | return 0; |
1521 | } | |
1da177e4 | 1522 | |
96b8e145 HV |
1523 | static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag) |
1524 | { | |
171f48e2 TP |
1525 | unsigned int num, i; |
1526 | ||
1527 | for (num = i = 0; i < NUM_FORMATS; i++) { | |
1528 | if (zoran_formats[i].flags & flag && num++ == fmt->index) { | |
1529 | strncpy(fmt->description, zoran_formats[i].name, | |
1530 | sizeof(fmt->description) - 1); | |
42b2aa86 | 1531 | /* fmt struct pre-zeroed, so adding '\0' not needed */ |
171f48e2 TP |
1532 | fmt->pixelformat = zoran_formats[i].fourcc; |
1533 | if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED) | |
1534 | fmt->flags |= V4L2_FMT_FLAG_COMPRESSED; | |
1535 | return 0; | |
1536 | } | |
1da177e4 | 1537 | } |
171f48e2 | 1538 | return -EINVAL; |
96b8e145 | 1539 | } |
1da177e4 | 1540 | |
96b8e145 HV |
1541 | static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh, |
1542 | struct v4l2_fmtdesc *f) | |
1543 | { | |
1544 | struct zoran_fh *fh = __fh; | |
1545 | struct zoran *zr = fh->zr; | |
1da177e4 | 1546 | |
96b8e145 HV |
1547 | return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE); |
1548 | } | |
1da177e4 | 1549 | |
96b8e145 HV |
1550 | static int zoran_enum_fmt_vid_out(struct file *file, void *__fh, |
1551 | struct v4l2_fmtdesc *f) | |
1552 | { | |
1553 | struct zoran_fh *fh = __fh; | |
1554 | struct zoran *zr = fh->zr; | |
1da177e4 | 1555 | |
96b8e145 HV |
1556 | return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK); |
1557 | } | |
1da177e4 | 1558 | |
96b8e145 HV |
1559 | static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh, |
1560 | struct v4l2_fmtdesc *f) | |
1561 | { | |
1562 | struct zoran_fh *fh = __fh; | |
1563 | struct zoran *zr = fh->zr; | |
1da177e4 | 1564 | |
96b8e145 HV |
1565 | return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY); |
1566 | } | |
1da177e4 | 1567 | |
96b8e145 HV |
1568 | static int zoran_g_fmt_vid_out(struct file *file, void *__fh, |
1569 | struct v4l2_format *fmt) | |
1570 | { | |
1571 | struct zoran_fh *fh = __fh; | |
1da177e4 | 1572 | |
96b8e145 | 1573 | fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm; |
5ca75b00 | 1574 | fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 / |
96b8e145 HV |
1575 | (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm); |
1576 | fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings); | |
1577 | fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG; | |
1578 | if (fh->jpg_settings.TmpDcm == 1) | |
1579 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
3e66793b | 1580 | V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT); |
96b8e145 HV |
1581 | else |
1582 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
1583 | V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM); | |
1584 | fmt->fmt.pix.bytesperline = 0; | |
1585 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1da177e4 | 1586 | |
96b8e145 HV |
1587 | return 0; |
1588 | } | |
1da177e4 | 1589 | |
96b8e145 HV |
1590 | static int zoran_g_fmt_vid_cap(struct file *file, void *__fh, |
1591 | struct v4l2_format *fmt) | |
1592 | { | |
1593 | struct zoran_fh *fh = __fh; | |
1594 | struct zoran *zr = fh->zr; | |
1da177e4 | 1595 | |
96b8e145 HV |
1596 | if (fh->map_mode != ZORAN_MAP_MODE_RAW) |
1597 | return zoran_g_fmt_vid_out(file, fh, fmt); | |
1da177e4 | 1598 | |
96b8e145 HV |
1599 | fmt->fmt.pix.width = fh->v4l_settings.width; |
1600 | fmt->fmt.pix.height = fh->v4l_settings.height; | |
1601 | fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline * | |
1602 | fh->v4l_settings.height; | |
1603 | fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc; | |
1604 | fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace; | |
1605 | fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline; | |
1606 | if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2)) | |
1607 | fmt->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
1608 | else | |
1609 | fmt->fmt.pix.field = V4L2_FIELD_TOP; | |
96b8e145 HV |
1610 | return 0; |
1611 | } | |
1da177e4 | 1612 | |
96b8e145 HV |
1613 | static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh, |
1614 | struct v4l2_format *fmt) | |
1615 | { | |
1616 | struct zoran_fh *fh = __fh; | |
1617 | struct zoran *zr = fh->zr; | |
1da177e4 | 1618 | |
96b8e145 HV |
1619 | fmt->fmt.win.w.left = fh->overlay_settings.x; |
1620 | fmt->fmt.win.w.top = fh->overlay_settings.y; | |
1621 | fmt->fmt.win.w.width = fh->overlay_settings.width; | |
1622 | fmt->fmt.win.w.height = fh->overlay_settings.height; | |
1623 | if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT) | |
1624 | fmt->fmt.win.field = V4L2_FIELD_INTERLACED; | |
1625 | else | |
1626 | fmt->fmt.win.field = V4L2_FIELD_TOP; | |
1da177e4 | 1627 | |
96b8e145 HV |
1628 | return 0; |
1629 | } | |
1da177e4 | 1630 | |
96b8e145 HV |
1631 | static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh, |
1632 | struct v4l2_format *fmt) | |
1633 | { | |
1634 | struct zoran_fh *fh = __fh; | |
1635 | struct zoran *zr = fh->zr; | |
1da177e4 | 1636 | |
96b8e145 HV |
1637 | if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH) |
1638 | fmt->fmt.win.w.width = BUZ_MAX_WIDTH; | |
1639 | if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH) | |
1640 | fmt->fmt.win.w.width = BUZ_MIN_WIDTH; | |
1641 | if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT) | |
1642 | fmt->fmt.win.w.height = BUZ_MAX_HEIGHT; | |
1643 | if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT) | |
1644 | fmt->fmt.win.w.height = BUZ_MIN_HEIGHT; | |
1da177e4 | 1645 | |
96b8e145 HV |
1646 | return 0; |
1647 | } | |
1da177e4 | 1648 | |
96b8e145 HV |
1649 | static int zoran_try_fmt_vid_out(struct file *file, void *__fh, |
1650 | struct v4l2_format *fmt) | |
1651 | { | |
1652 | struct zoran_fh *fh = __fh; | |
1653 | struct zoran *zr = fh->zr; | |
1654 | struct zoran_jpg_settings settings; | |
1655 | int res = 0; | |
1da177e4 | 1656 | |
96b8e145 HV |
1657 | if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG) |
1658 | return -EINVAL; | |
1da177e4 | 1659 | |
96b8e145 | 1660 | settings = fh->jpg_settings; |
1da177e4 | 1661 | |
96b8e145 HV |
1662 | /* we actually need to set 'real' parameters now */ |
1663 | if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT) | |
1664 | settings.TmpDcm = 1; | |
1665 | else | |
1666 | settings.TmpDcm = 2; | |
1667 | settings.decimation = 0; | |
1668 | if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2) | |
1669 | settings.VerDcm = 2; | |
1670 | else | |
1671 | settings.VerDcm = 1; | |
1672 | if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4) | |
1673 | settings.HorDcm = 4; | |
1674 | else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2) | |
1675 | settings.HorDcm = 2; | |
1676 | else | |
1677 | settings.HorDcm = 1; | |
1678 | if (settings.TmpDcm == 1) | |
1679 | settings.field_per_buff = 2; | |
1680 | else | |
1681 | settings.field_per_buff = 1; | |
1682 | ||
886fe23d HV |
1683 | if (settings.HorDcm > 1) { |
1684 | settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0; | |
1685 | settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH; | |
1686 | } else { | |
1687 | settings.img_x = 0; | |
1688 | settings.img_width = BUZ_MAX_WIDTH; | |
1689 | } | |
1690 | ||
96b8e145 | 1691 | /* check */ |
0ba514d5 | 1692 | res = zoran_check_jpg_settings(zr, &settings, 1); |
96b8e145 | 1693 | if (res) |
7b962d43 | 1694 | return res; |
96b8e145 HV |
1695 | |
1696 | /* tell the user what we actually did */ | |
1697 | fmt->fmt.pix.width = settings.img_width / settings.HorDcm; | |
1698 | fmt->fmt.pix.height = settings.img_height * 2 / | |
1699 | (settings.TmpDcm * settings.VerDcm); | |
1700 | if (settings.TmpDcm == 1) | |
1701 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
1702 | V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT); | |
1703 | else | |
1704 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
1705 | V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM); | |
1da177e4 | 1706 | |
96b8e145 | 1707 | fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings); |
886fe23d HV |
1708 | fmt->fmt.pix.bytesperline = 0; |
1709 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
96b8e145 HV |
1710 | return res; |
1711 | } | |
1da177e4 | 1712 | |
96b8e145 HV |
1713 | static int zoran_try_fmt_vid_cap(struct file *file, void *__fh, |
1714 | struct v4l2_format *fmt) | |
1715 | { | |
1716 | struct zoran_fh *fh = __fh; | |
1717 | struct zoran *zr = fh->zr; | |
0ba514d5 | 1718 | int bpp; |
96b8e145 | 1719 | int i; |
1da177e4 | 1720 | |
96b8e145 HV |
1721 | if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) |
1722 | return zoran_try_fmt_vid_out(file, fh, fmt); | |
1da177e4 | 1723 | |
96b8e145 HV |
1724 | for (i = 0; i < NUM_FORMATS; i++) |
1725 | if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat) | |
1726 | break; | |
1da177e4 | 1727 | |
7b962d43 | 1728 | if (i == NUM_FORMATS) |
96b8e145 | 1729 | return -EINVAL; |
1da177e4 | 1730 | |
728f5b93 TP |
1731 | bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8); |
1732 | v4l_bound_align_image( | |
1733 | &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2, | |
1734 | &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0); | |
96b8e145 HV |
1735 | return 0; |
1736 | } | |
1da177e4 | 1737 | |
96b8e145 HV |
1738 | static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh, |
1739 | struct v4l2_format *fmt) | |
1740 | { | |
1741 | struct zoran_fh *fh = __fh; | |
96b8e145 HV |
1742 | int res; |
1743 | ||
1744 | dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n", | |
1745 | fmt->fmt.win.w.left, fmt->fmt.win.w.top, | |
1746 | fmt->fmt.win.w.width, | |
1747 | fmt->fmt.win.w.height, | |
1748 | fmt->fmt.win.clipcount, | |
1749 | fmt->fmt.win.bitmap); | |
e5ee3f64 TP |
1750 | res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top, |
1751 | fmt->fmt.win.w.width, fmt->fmt.win.w.height, | |
1752 | (struct v4l2_clip __user *)fmt->fmt.win.clips, | |
1753 | fmt->fmt.win.clipcount, fmt->fmt.win.bitmap); | |
96b8e145 HV |
1754 | return res; |
1755 | } | |
1da177e4 | 1756 | |
96b8e145 HV |
1757 | static int zoran_s_fmt_vid_out(struct file *file, void *__fh, |
1758 | struct v4l2_format *fmt) | |
1759 | { | |
1760 | struct zoran_fh *fh = __fh; | |
1761 | struct zoran *zr = fh->zr; | |
1762 | __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat); | |
1763 | struct zoran_jpg_settings settings; | |
1764 | int res = 0; | |
1da177e4 | 1765 | |
96b8e145 HV |
1766 | dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n", |
1767 | fmt->fmt.pix.width, fmt->fmt.pix.height, | |
1768 | fmt->fmt.pix.pixelformat, | |
1769 | (char *) &printformat); | |
1770 | if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG) | |
1771 | return -EINVAL; | |
1da177e4 | 1772 | |
1159b7f1 | 1773 | if (fh->buffers.allocated) { |
96b8e145 | 1774 | dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n", |
1159b7f1 | 1775 | ZR_DEVNAME(zr)); |
96b8e145 | 1776 | res = -EBUSY; |
7b962d43 | 1777 | return res; |
96b8e145 | 1778 | } |
1da177e4 | 1779 | |
1159b7f1 TP |
1780 | settings = fh->jpg_settings; |
1781 | ||
96b8e145 | 1782 | /* we actually need to set 'real' parameters now */ |
7f37cc9b | 1783 | if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT) |
96b8e145 HV |
1784 | settings.TmpDcm = 1; |
1785 | else | |
1786 | settings.TmpDcm = 2; | |
1787 | settings.decimation = 0; | |
1788 | if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2) | |
1789 | settings.VerDcm = 2; | |
1790 | else | |
1791 | settings.VerDcm = 1; | |
1792 | if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4) | |
1793 | settings.HorDcm = 4; | |
1794 | else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2) | |
1795 | settings.HorDcm = 2; | |
1796 | else | |
1797 | settings.HorDcm = 1; | |
1798 | if (settings.TmpDcm == 1) | |
1799 | settings.field_per_buff = 2; | |
1800 | else | |
1801 | settings.field_per_buff = 1; | |
1802 | ||
0ba514d5 HV |
1803 | if (settings.HorDcm > 1) { |
1804 | settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0; | |
1805 | settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH; | |
1806 | } else { | |
1807 | settings.img_x = 0; | |
1808 | settings.img_width = BUZ_MAX_WIDTH; | |
1809 | } | |
1810 | ||
96b8e145 | 1811 | /* check */ |
0ba514d5 | 1812 | res = zoran_check_jpg_settings(zr, &settings, 0); |
96b8e145 | 1813 | if (res) |
7b962d43 | 1814 | return res; |
96b8e145 HV |
1815 | |
1816 | /* it's ok, so set them */ | |
1817 | fh->jpg_settings = settings; | |
1818 | ||
1159b7f1 TP |
1819 | map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT); |
1820 | fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings); | |
1821 | ||
96b8e145 HV |
1822 | /* tell the user what we actually did */ |
1823 | fmt->fmt.pix.width = settings.img_width / settings.HorDcm; | |
1824 | fmt->fmt.pix.height = settings.img_height * 2 / | |
1825 | (settings.TmpDcm * settings.VerDcm); | |
1826 | if (settings.TmpDcm == 1) | |
1827 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
1828 | V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT); | |
1829 | else | |
1830 | fmt->fmt.pix.field = (fh->jpg_settings.odd_even ? | |
1831 | V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM); | |
96b8e145 | 1832 | fmt->fmt.pix.bytesperline = 0; |
1159b7f1 | 1833 | fmt->fmt.pix.sizeimage = fh->buffers.buffer_size; |
96b8e145 | 1834 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
96b8e145 HV |
1835 | return res; |
1836 | } | |
1da177e4 | 1837 | |
96b8e145 HV |
1838 | static int zoran_s_fmt_vid_cap(struct file *file, void *__fh, |
1839 | struct v4l2_format *fmt) | |
1840 | { | |
1841 | struct zoran_fh *fh = __fh; | |
1842 | struct zoran *zr = fh->zr; | |
1843 | int i; | |
1844 | int res = 0; | |
1da177e4 | 1845 | |
96b8e145 HV |
1846 | if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) |
1847 | return zoran_s_fmt_vid_out(file, fh, fmt); | |
1da177e4 | 1848 | |
96b8e145 HV |
1849 | for (i = 0; i < NUM_FORMATS; i++) |
1850 | if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc) | |
1851 | break; | |
1852 | if (i == NUM_FORMATS) { | |
1853 | dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n", | |
1854 | ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat); | |
1855 | return -EINVAL; | |
1da177e4 | 1856 | } |
1159b7f1 | 1857 | |
1159b7f1 TP |
1858 | if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) || |
1859 | fh->buffers.active != ZORAN_FREE) { | |
96b8e145 HV |
1860 | dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n", |
1861 | ZR_DEVNAME(zr)); | |
1862 | res = -EBUSY; | |
7b962d43 | 1863 | return res; |
96b8e145 HV |
1864 | } |
1865 | if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT) | |
1866 | fmt->fmt.pix.height = BUZ_MAX_HEIGHT; | |
1867 | if (fmt->fmt.pix.width > BUZ_MAX_WIDTH) | |
1868 | fmt->fmt.pix.width = BUZ_MAX_WIDTH; | |
1869 | ||
1159b7f1 TP |
1870 | map_mode_raw(fh); |
1871 | ||
1872 | res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height, | |
1873 | &zoran_formats[i]); | |
96b8e145 | 1874 | if (res) |
7b962d43 | 1875 | return res; |
96b8e145 | 1876 | |
1159b7f1 | 1877 | /* tell the user the results/missing stuff */ |
96b8e145 HV |
1878 | fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline; |
1879 | fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline; | |
1880 | fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace; | |
1881 | if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2)) | |
1882 | fmt->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
1883 | else | |
1884 | fmt->fmt.pix.field = V4L2_FIELD_TOP; | |
96b8e145 HV |
1885 | return res; |
1886 | } | |
1da177e4 | 1887 | |
96b8e145 HV |
1888 | static int zoran_g_fbuf(struct file *file, void *__fh, |
1889 | struct v4l2_framebuffer *fb) | |
1890 | { | |
1891 | struct zoran_fh *fh = __fh; | |
1892 | struct zoran *zr = fh->zr; | |
1da177e4 | 1893 | |
96b8e145 | 1894 | memset(fb, 0, sizeof(*fb)); |
7f6adeaf HV |
1895 | fb->base = zr->vbuf_base; |
1896 | fb->fmt.width = zr->vbuf_width; | |
1897 | fb->fmt.height = zr->vbuf_height; | |
96b8e145 HV |
1898 | if (zr->overlay_settings.format) |
1899 | fb->fmt.pixelformat = fh->overlay_settings.format->fourcc; | |
7f6adeaf | 1900 | fb->fmt.bytesperline = zr->vbuf_bytesperline; |
96b8e145 HV |
1901 | fb->fmt.colorspace = V4L2_COLORSPACE_SRGB; |
1902 | fb->fmt.field = V4L2_FIELD_INTERLACED; | |
96b8e145 | 1903 | fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING; |
1da177e4 | 1904 | |
96b8e145 HV |
1905 | return 0; |
1906 | } | |
1da177e4 | 1907 | |
96b8e145 | 1908 | static int zoran_s_fbuf(struct file *file, void *__fh, |
e6eb28c2 | 1909 | const struct v4l2_framebuffer *fb) |
96b8e145 HV |
1910 | { |
1911 | struct zoran_fh *fh = __fh; | |
1912 | struct zoran *zr = fh->zr; | |
1913 | int i, res = 0; | |
1914 | __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat); | |
1da177e4 | 1915 | |
96b8e145 HV |
1916 | for (i = 0; i < NUM_FORMATS; i++) |
1917 | if (zoran_formats[i].fourcc == fb->fmt.pixelformat) | |
1da177e4 | 1918 | break; |
96b8e145 HV |
1919 | if (i == NUM_FORMATS) { |
1920 | dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n", | |
1921 | ZR_DEVNAME(zr), fb->fmt.pixelformat, | |
1922 | (char *)&printformat); | |
1923 | return -EINVAL; | |
1da177e4 | 1924 | } |
1da177e4 | 1925 | |
e5ee3f64 TP |
1926 | res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width, |
1927 | fb->fmt.height, fb->fmt.bytesperline); | |
1da177e4 | 1928 | |
96b8e145 HV |
1929 | return res; |
1930 | } | |
1da177e4 | 1931 | |
96b8e145 HV |
1932 | static int zoran_overlay(struct file *file, void *__fh, unsigned int on) |
1933 | { | |
1934 | struct zoran_fh *fh = __fh; | |
96b8e145 | 1935 | int res; |
1da177e4 | 1936 | |
e5ee3f64 | 1937 | res = setup_overlay(fh, on); |
1da177e4 | 1938 | |
96b8e145 HV |
1939 | return res; |
1940 | } | |
1da177e4 | 1941 | |
84c1b094 HV |
1942 | static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type); |
1943 | ||
96b8e145 HV |
1944 | static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req) |
1945 | { | |
1946 | struct zoran_fh *fh = __fh; | |
1947 | struct zoran *zr = fh->zr; | |
1948 | int res = 0; | |
1da177e4 | 1949 | |
96b8e145 | 1950 | if (req->memory != V4L2_MEMORY_MMAP) { |
84c1b094 | 1951 | dprintk(2, |
96b8e145 HV |
1952 | KERN_ERR |
1953 | "%s: only MEMORY_MMAP capture is supported, not %d\n", | |
1954 | ZR_DEVNAME(zr), req->memory); | |
1955 | return -EINVAL; | |
1956 | } | |
1da177e4 | 1957 | |
84c1b094 HV |
1958 | if (req->count == 0) |
1959 | return zoran_streamoff(file, fh, req->type); | |
1da177e4 | 1960 | |
1159b7f1 | 1961 | if (fh->buffers.allocated) { |
84c1b094 | 1962 | dprintk(2, |
1da177e4 | 1963 | KERN_ERR |
7f37cc9b | 1964 | "%s: VIDIOC_REQBUFS - buffers already allocated\n", |
96b8e145 HV |
1965 | ZR_DEVNAME(zr)); |
1966 | res = -EBUSY; | |
7b962d43 | 1967 | return res; |
1da177e4 | 1968 | } |
1da177e4 | 1969 | |
96b8e145 | 1970 | if (fh->map_mode == ZORAN_MAP_MODE_RAW && |
1159b7f1 | 1971 | req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
96b8e145 HV |
1972 | /* control user input */ |
1973 | if (req->count < 2) | |
1974 | req->count = 2; | |
1975 | if (req->count > v4l_nbufs) | |
1976 | req->count = v4l_nbufs; | |
1159b7f1 TP |
1977 | |
1978 | /* The next mmap will map the V4L buffers */ | |
1979 | map_mode_raw(fh); | |
1980 | fh->buffers.num_buffers = req->count; | |
1da177e4 | 1981 | |
e5ee3f64 | 1982 | if (v4l_fbuffer_alloc(fh)) { |
96b8e145 | 1983 | res = -ENOMEM; |
7b962d43 | 1984 | return res; |
96b8e145 | 1985 | } |
96b8e145 | 1986 | } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC || |
1159b7f1 | 1987 | fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) { |
96b8e145 HV |
1988 | /* we need to calculate size ourselves now */ |
1989 | if (req->count < 4) | |
1990 | req->count = 4; | |
1991 | if (req->count > jpg_nbufs) | |
1992 | req->count = jpg_nbufs; | |
1159b7f1 TP |
1993 | |
1994 | /* The next mmap will map the MJPEG buffers */ | |
1995 | map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT); | |
1996 | fh->buffers.num_buffers = req->count; | |
1997 | fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings); | |
1da177e4 | 1998 | |
e5ee3f64 | 1999 | if (jpg_fbuffer_alloc(fh)) { |
96b8e145 | 2000 | res = -ENOMEM; |
7b962d43 | 2001 | return res; |
96b8e145 | 2002 | } |
96b8e145 HV |
2003 | } else { |
2004 | dprintk(1, | |
1da177e4 | 2005 | KERN_ERR |
96b8e145 HV |
2006 | "%s: VIDIOC_REQBUFS - unknown type %d\n", |
2007 | ZR_DEVNAME(zr), req->type); | |
2008 | res = -EINVAL; | |
7b962d43 | 2009 | return res; |
1da177e4 | 2010 | } |
96b8e145 HV |
2011 | return res; |
2012 | } | |
1da177e4 | 2013 | |
96b8e145 HV |
2014 | static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf) |
2015 | { | |
2016 | struct zoran_fh *fh = __fh; | |
9cfb6a3f | 2017 | int res; |
1da177e4 | 2018 | |
e276f7b5 | 2019 | res = zoran_v4l2_buffer_status(fh, buf, buf->index); |
1da177e4 | 2020 | |
96b8e145 HV |
2021 | return res; |
2022 | } | |
1da177e4 | 2023 | |
96b8e145 HV |
2024 | static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf) |
2025 | { | |
2026 | struct zoran_fh *fh = __fh; | |
2027 | struct zoran *zr = fh->zr; | |
2028 | int res = 0, codec_mode, buf_type; | |
1da177e4 | 2029 | |
96b8e145 HV |
2030 | switch (fh->map_mode) { |
2031 | case ZORAN_MAP_MODE_RAW: | |
2032 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
2033 | dprintk(1, KERN_ERR | |
2034 | "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n", | |
2035 | ZR_DEVNAME(zr), buf->type, fh->map_mode); | |
1da177e4 | 2036 | res = -EINVAL; |
7b962d43 | 2037 | return res; |
1da177e4 | 2038 | } |
1da177e4 | 2039 | |
e5ee3f64 | 2040 | res = zoran_v4l_queue_frame(fh, buf->index); |
96b8e145 | 2041 | if (res) |
7b962d43 | 2042 | return res; |
1159b7f1 | 2043 | if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED) |
96b8e145 | 2044 | zr36057_set_memgrab(zr, 1); |
1da177e4 LT |
2045 | break; |
2046 | ||
96b8e145 HV |
2047 | case ZORAN_MAP_MODE_JPG_REC: |
2048 | case ZORAN_MAP_MODE_JPG_PLAY: | |
2049 | if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) { | |
2050 | buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
2051 | codec_mode = BUZ_MODE_MOTION_DECOMPRESS; | |
2052 | } else { | |
2053 | buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
2054 | codec_mode = BUZ_MODE_MOTION_COMPRESS; | |
1da177e4 LT |
2055 | } |
2056 | ||
96b8e145 HV |
2057 | if (buf->type != buf_type) { |
2058 | dprintk(1, KERN_ERR | |
2059 | "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n", | |
2060 | ZR_DEVNAME(zr), buf->type, fh->map_mode); | |
2061 | res = -EINVAL; | |
7b962d43 | 2062 | return res; |
1da177e4 LT |
2063 | } |
2064 | ||
e5ee3f64 | 2065 | res = zoran_jpg_queue_frame(fh, buf->index, codec_mode); |
96b8e145 | 2066 | if (res != 0) |
7b962d43 | 2067 | return res; |
96b8e145 | 2068 | if (zr->codec_mode == BUZ_MODE_IDLE && |
1159b7f1 | 2069 | fh->buffers.active == ZORAN_LOCKED) |
96b8e145 | 2070 | zr36057_enable_jpg(zr, codec_mode); |
1159b7f1 | 2071 | |
96b8e145 | 2072 | break; |
1da177e4 | 2073 | |
96b8e145 HV |
2074 | default: |
2075 | dprintk(1, KERN_ERR | |
2076 | "%s: VIDIOC_QBUF - unsupported type %d\n", | |
2077 | ZR_DEVNAME(zr), buf->type); | |
2078 | res = -EINVAL; | |
1da177e4 | 2079 | break; |
96b8e145 | 2080 | } |
96b8e145 HV |
2081 | return res; |
2082 | } | |
1da177e4 | 2083 | |
96b8e145 HV |
2084 | static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf) |
2085 | { | |
2086 | struct zoran_fh *fh = __fh; | |
2087 | struct zoran *zr = fh->zr; | |
2088 | int res = 0, buf_type, num = -1; /* compiler borks here (?) */ | |
1da177e4 | 2089 | |
96b8e145 HV |
2090 | switch (fh->map_mode) { |
2091 | case ZORAN_MAP_MODE_RAW: | |
2092 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
2093 | dprintk(1, KERN_ERR | |
2094 | "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n", | |
2095 | ZR_DEVNAME(zr), buf->type, fh->map_mode); | |
2096 | res = -EINVAL; | |
7b962d43 | 2097 | return res; |
1da177e4 LT |
2098 | } |
2099 | ||
96b8e145 HV |
2100 | num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME]; |
2101 | if (file->f_flags & O_NONBLOCK && | |
2102 | zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) { | |
2103 | res = -EAGAIN; | |
7b962d43 | 2104 | return res; |
1da177e4 | 2105 | } |
e5ee3f64 | 2106 | res = v4l_sync(fh, num); |
96b8e145 | 2107 | if (res) |
7b962d43 | 2108 | return res; |
96b8e145 | 2109 | zr->v4l_sync_tail++; |
e276f7b5 | 2110 | res = zoran_v4l2_buffer_status(fh, buf, num); |
1da177e4 LT |
2111 | break; |
2112 | ||
96b8e145 HV |
2113 | case ZORAN_MAP_MODE_JPG_REC: |
2114 | case ZORAN_MAP_MODE_JPG_PLAY: | |
1da177e4 | 2115 | { |
96b8e145 | 2116 | struct zoran_sync bs; |
1da177e4 | 2117 | |
96b8e145 HV |
2118 | if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) |
2119 | buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT; | |
2120 | else | |
2121 | buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1da177e4 | 2122 | |
96b8e145 HV |
2123 | if (buf->type != buf_type) { |
2124 | dprintk(1, KERN_ERR | |
2125 | "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n", | |
2126 | ZR_DEVNAME(zr), buf->type, fh->map_mode); | |
2127 | res = -EINVAL; | |
7b962d43 | 2128 | return res; |
1da177e4 LT |
2129 | } |
2130 | ||
96b8e145 | 2131 | num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME]; |
1da177e4 | 2132 | |
96b8e145 HV |
2133 | if (file->f_flags & O_NONBLOCK && |
2134 | zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) { | |
2135 | res = -EAGAIN; | |
7b962d43 | 2136 | return res; |
96b8e145 | 2137 | } |
1a5e5af0 | 2138 | bs.frame = 0; /* suppress compiler warning */ |
e5ee3f64 | 2139 | res = jpg_sync(fh, &bs); |
96b8e145 | 2140 | if (res) |
7b962d43 | 2141 | return res; |
e276f7b5 | 2142 | res = zoran_v4l2_buffer_status(fh, buf, bs.frame); |
96b8e145 | 2143 | break; |
1da177e4 | 2144 | } |
96b8e145 HV |
2145 | |
2146 | default: | |
2147 | dprintk(1, KERN_ERR | |
2148 | "%s: VIDIOC_DQBUF - unsupported type %d\n", | |
2149 | ZR_DEVNAME(zr), buf->type); | |
2150 | res = -EINVAL; | |
1da177e4 | 2151 | break; |
96b8e145 | 2152 | } |
96b8e145 HV |
2153 | return res; |
2154 | } | |
1da177e4 | 2155 | |
96b8e145 HV |
2156 | static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type) |
2157 | { | |
2158 | struct zoran_fh *fh = __fh; | |
2159 | struct zoran *zr = fh->zr; | |
2160 | int res = 0; | |
1da177e4 | 2161 | |
96b8e145 HV |
2162 | switch (fh->map_mode) { |
2163 | case ZORAN_MAP_MODE_RAW: /* raw capture */ | |
2164 | if (zr->v4l_buffers.active != ZORAN_ACTIVE || | |
1159b7f1 | 2165 | fh->buffers.active != ZORAN_ACTIVE) { |
96b8e145 | 2166 | res = -EBUSY; |
7b962d43 | 2167 | return res; |
1da177e4 LT |
2168 | } |
2169 | ||
1159b7f1 | 2170 | zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED; |
96b8e145 HV |
2171 | zr->v4l_settings = fh->v4l_settings; |
2172 | ||
2173 | zr->v4l_sync_tail = zr->v4l_pend_tail; | |
2174 | if (!zr->v4l_memgrab_active && | |
2175 | zr->v4l_pend_head != zr->v4l_pend_tail) { | |
2176 | zr36057_set_memgrab(zr, 1); | |
2177 | } | |
1da177e4 LT |
2178 | break; |
2179 | ||
96b8e145 HV |
2180 | case ZORAN_MAP_MODE_JPG_REC: |
2181 | case ZORAN_MAP_MODE_JPG_PLAY: | |
2182 | /* what is the codec mode right now? */ | |
2183 | if (zr->jpg_buffers.active != ZORAN_ACTIVE || | |
1159b7f1 | 2184 | fh->buffers.active != ZORAN_ACTIVE) { |
96b8e145 | 2185 | res = -EBUSY; |
7b962d43 | 2186 | return res; |
96b8e145 | 2187 | } |
1da177e4 | 2188 | |
1159b7f1 | 2189 | zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED; |
1da177e4 | 2190 | |
96b8e145 HV |
2191 | if (zr->jpg_que_head != zr->jpg_que_tail) { |
2192 | /* Start the jpeg codec when the first frame is queued */ | |
2193 | jpeg_start(zr); | |
1da177e4 | 2194 | } |
96b8e145 | 2195 | break; |
1da177e4 | 2196 | |
96b8e145 HV |
2197 | default: |
2198 | dprintk(1, | |
2199 | KERN_ERR | |
2200 | "%s: VIDIOC_STREAMON - invalid map mode %d\n", | |
2201 | ZR_DEVNAME(zr), fh->map_mode); | |
2202 | res = -EINVAL; | |
1da177e4 | 2203 | break; |
96b8e145 | 2204 | } |
96b8e145 HV |
2205 | return res; |
2206 | } | |
2207 | ||
2208 | static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type) | |
2209 | { | |
2210 | struct zoran_fh *fh = __fh; | |
2211 | struct zoran *zr = fh->zr; | |
2212 | int i, res = 0; | |
1159b7f1 | 2213 | unsigned long flags; |
1da177e4 | 2214 | |
96b8e145 HV |
2215 | switch (fh->map_mode) { |
2216 | case ZORAN_MAP_MODE_RAW: /* raw capture */ | |
1159b7f1 | 2217 | if (fh->buffers.active == ZORAN_FREE && |
96b8e145 HV |
2218 | zr->v4l_buffers.active != ZORAN_FREE) { |
2219 | res = -EPERM; /* stay off other's settings! */ | |
7b962d43 | 2220 | return res; |
1da177e4 | 2221 | } |
96b8e145 | 2222 | if (zr->v4l_buffers.active == ZORAN_FREE) |
7b962d43 | 2223 | return res; |
1da177e4 | 2224 | |
1159b7f1 | 2225 | spin_lock_irqsave(&zr->spinlock, flags); |
96b8e145 HV |
2226 | /* unload capture */ |
2227 | if (zr->v4l_memgrab_active) { | |
1da177e4 | 2228 | |
96b8e145 | 2229 | zr36057_set_memgrab(zr, 0); |
1da177e4 | 2230 | } |
1da177e4 | 2231 | |
1159b7f1 | 2232 | for (i = 0; i < fh->buffers.num_buffers; i++) |
96b8e145 | 2233 | zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; |
1159b7f1 | 2234 | fh->buffers = zr->v4l_buffers; |
1da177e4 | 2235 | |
1159b7f1 | 2236 | zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE; |
1da177e4 | 2237 | |
96b8e145 HV |
2238 | zr->v4l_grab_seq = 0; |
2239 | zr->v4l_pend_head = zr->v4l_pend_tail = 0; | |
2240 | zr->v4l_sync_tail = 0; | |
1da177e4 | 2241 | |
1159b7f1 TP |
2242 | spin_unlock_irqrestore(&zr->spinlock, flags); |
2243 | ||
96b8e145 | 2244 | break; |
1da177e4 | 2245 | |
96b8e145 HV |
2246 | case ZORAN_MAP_MODE_JPG_REC: |
2247 | case ZORAN_MAP_MODE_JPG_PLAY: | |
1159b7f1 | 2248 | if (fh->buffers.active == ZORAN_FREE && |
96b8e145 HV |
2249 | zr->jpg_buffers.active != ZORAN_FREE) { |
2250 | res = -EPERM; /* stay off other's settings! */ | |
7b962d43 | 2251 | return res; |
96b8e145 HV |
2252 | } |
2253 | if (zr->jpg_buffers.active == ZORAN_FREE) | |
7b962d43 | 2254 | return res; |
96b8e145 | 2255 | |
e5ee3f64 | 2256 | res = jpg_qbuf(fh, -1, |
96b8e145 HV |
2257 | (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ? |
2258 | BUZ_MODE_MOTION_COMPRESS : | |
2259 | BUZ_MODE_MOTION_DECOMPRESS); | |
2260 | if (res) | |
7b962d43 | 2261 | return res; |
96b8e145 HV |
2262 | break; |
2263 | default: | |
2264 | dprintk(1, KERN_ERR | |
2265 | "%s: VIDIOC_STREAMOFF - invalid map mode %d\n", | |
2266 | ZR_DEVNAME(zr), fh->map_mode); | |
2267 | res = -EINVAL; | |
1da177e4 | 2268 | break; |
96b8e145 | 2269 | } |
96b8e145 HV |
2270 | return res; |
2271 | } | |
96b8e145 HV |
2272 | static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std) |
2273 | { | |
2274 | struct zoran_fh *fh = __fh; | |
2275 | struct zoran *zr = fh->zr; | |
96b8e145 | 2276 | |
107063c6 | 2277 | *std = zr->norm; |
96b8e145 HV |
2278 | return 0; |
2279 | } | |
1da177e4 | 2280 | |
314527ac | 2281 | static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id std) |
96b8e145 HV |
2282 | { |
2283 | struct zoran_fh *fh = __fh; | |
2284 | struct zoran *zr = fh->zr; | |
107063c6 | 2285 | int res = 0; |
1da177e4 | 2286 | |
314527ac | 2287 | res = zoran_set_norm(zr, std); |
96b8e145 | 2288 | if (res) |
7b962d43 | 2289 | return res; |
1da177e4 | 2290 | |
96b8e145 | 2291 | res = wait_grab_pending(zr); |
96b8e145 HV |
2292 | return res; |
2293 | } | |
1da177e4 | 2294 | |
96b8e145 HV |
2295 | static int zoran_enum_input(struct file *file, void *__fh, |
2296 | struct v4l2_input *inp) | |
2297 | { | |
2298 | struct zoran_fh *fh = __fh; | |
2299 | struct zoran *zr = fh->zr; | |
1da177e4 | 2300 | |
f14a2972 | 2301 | if (inp->index >= zr->card.inputs) |
96b8e145 | 2302 | return -EINVAL; |
1da177e4 | 2303 | |
96b8e145 HV |
2304 | strncpy(inp->name, zr->card.input[inp->index].name, |
2305 | sizeof(inp->name) - 1); | |
2306 | inp->type = V4L2_INPUT_TYPE_CAMERA; | |
2307 | inp->std = V4L2_STD_ALL; | |
1da177e4 | 2308 | |
96b8e145 | 2309 | /* Get status of video decoder */ |
0ab6e1c3 | 2310 | decoder_call(zr, video, g_input_status, &inp->status); |
96b8e145 HV |
2311 | return 0; |
2312 | } | |
1da177e4 | 2313 | |
96b8e145 HV |
2314 | static int zoran_g_input(struct file *file, void *__fh, unsigned int *input) |
2315 | { | |
2316 | struct zoran_fh *fh = __fh; | |
2317 | struct zoran *zr = fh->zr; | |
1da177e4 | 2318 | |
96b8e145 | 2319 | *input = zr->input; |
1da177e4 | 2320 | |
96b8e145 HV |
2321 | return 0; |
2322 | } | |
1da177e4 | 2323 | |
96b8e145 HV |
2324 | static int zoran_s_input(struct file *file, void *__fh, unsigned int input) |
2325 | { | |
2326 | struct zoran_fh *fh = __fh; | |
2327 | struct zoran *zr = fh->zr; | |
2328 | int res; | |
1da177e4 | 2329 | |
96b8e145 HV |
2330 | res = zoran_set_input(zr, input); |
2331 | if (res) | |
7b962d43 | 2332 | return res; |
1da177e4 | 2333 | |
96b8e145 HV |
2334 | /* Make sure the changes come into effect */ |
2335 | res = wait_grab_pending(zr); | |
96b8e145 HV |
2336 | return res; |
2337 | } | |
1da177e4 | 2338 | |
96b8e145 HV |
2339 | static int zoran_enum_output(struct file *file, void *__fh, |
2340 | struct v4l2_output *outp) | |
2341 | { | |
2342 | if (outp->index != 0) | |
2343 | return -EINVAL; | |
1da177e4 | 2344 | |
96b8e145 HV |
2345 | outp->index = 0; |
2346 | outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY; | |
2347 | strncpy(outp->name, "Autodetect", sizeof(outp->name)-1); | |
1da177e4 | 2348 | |
96b8e145 HV |
2349 | return 0; |
2350 | } | |
1da177e4 | 2351 | |
96b8e145 HV |
2352 | static int zoran_g_output(struct file *file, void *__fh, unsigned int *output) |
2353 | { | |
2354 | *output = 0; | |
2355 | ||
2356 | return 0; | |
2357 | } | |
1da177e4 | 2358 | |
96b8e145 HV |
2359 | static int zoran_s_output(struct file *file, void *__fh, unsigned int output) |
2360 | { | |
2361 | if (output != 0) | |
2362 | return -EINVAL; | |
1da177e4 | 2363 | |
96b8e145 HV |
2364 | return 0; |
2365 | } | |
1da177e4 | 2366 | |
96b8e145 HV |
2367 | /* cropping (sub-frame capture) */ |
2368 | static int zoran_cropcap(struct file *file, void *__fh, | |
2369 | struct v4l2_cropcap *cropcap) | |
2370 | { | |
2371 | struct zoran_fh *fh = __fh; | |
2372 | struct zoran *zr = fh->zr; | |
2373 | int type = cropcap->type, res = 0; | |
1da177e4 | 2374 | |
96b8e145 HV |
2375 | memset(cropcap, 0, sizeof(*cropcap)); |
2376 | cropcap->type = type; | |
1da177e4 | 2377 | |
96b8e145 HV |
2378 | if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && |
2379 | (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
2380 | fh->map_mode == ZORAN_MAP_MODE_RAW)) { | |
2381 | dprintk(1, KERN_ERR | |
2382 | "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n", | |
1da177e4 | 2383 | ZR_DEVNAME(zr)); |
96b8e145 | 2384 | res = -EINVAL; |
7b962d43 | 2385 | return res; |
96b8e145 | 2386 | } |
1da177e4 | 2387 | |
96b8e145 HV |
2388 | cropcap->bounds.top = cropcap->bounds.left = 0; |
2389 | cropcap->bounds.width = BUZ_MAX_WIDTH; | |
2390 | cropcap->bounds.height = BUZ_MAX_HEIGHT; | |
2391 | cropcap->defrect.top = cropcap->defrect.left = 0; | |
2392 | cropcap->defrect.width = BUZ_MIN_WIDTH; | |
2393 | cropcap->defrect.height = BUZ_MIN_HEIGHT; | |
96b8e145 HV |
2394 | return res; |
2395 | } | |
1da177e4 | 2396 | |
96b8e145 HV |
2397 | static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop) |
2398 | { | |
2399 | struct zoran_fh *fh = __fh; | |
2400 | struct zoran *zr = fh->zr; | |
2401 | int type = crop->type, res = 0; | |
1da177e4 | 2402 | |
96b8e145 HV |
2403 | memset(crop, 0, sizeof(*crop)); |
2404 | crop->type = type; | |
1da177e4 | 2405 | |
96b8e145 HV |
2406 | if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && |
2407 | (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
2408 | fh->map_mode == ZORAN_MAP_MODE_RAW)) { | |
2409 | dprintk(1, | |
2410 | KERN_ERR | |
2411 | "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n", | |
2412 | ZR_DEVNAME(zr)); | |
2413 | res = -EINVAL; | |
7b962d43 | 2414 | return res; |
1da177e4 | 2415 | } |
1da177e4 | 2416 | |
96b8e145 HV |
2417 | crop->c.top = fh->jpg_settings.img_y; |
2418 | crop->c.left = fh->jpg_settings.img_x; | |
2419 | crop->c.width = fh->jpg_settings.img_width; | |
2420 | crop->c.height = fh->jpg_settings.img_height; | |
96b8e145 HV |
2421 | return res; |
2422 | } | |
1da177e4 | 2423 | |
4f996594 | 2424 | static int zoran_s_crop(struct file *file, void *__fh, const struct v4l2_crop *crop) |
96b8e145 HV |
2425 | { |
2426 | struct zoran_fh *fh = __fh; | |
2427 | struct zoran *zr = fh->zr; | |
2428 | int res = 0; | |
2429 | struct zoran_jpg_settings settings; | |
1da177e4 | 2430 | |
96b8e145 | 2431 | settings = fh->jpg_settings; |
1da177e4 | 2432 | |
1159b7f1 | 2433 | if (fh->buffers.allocated) { |
96b8e145 HV |
2434 | dprintk(1, KERN_ERR |
2435 | "%s: VIDIOC_S_CROP - cannot change settings while active\n", | |
2436 | ZR_DEVNAME(zr)); | |
2437 | res = -EBUSY; | |
7b962d43 | 2438 | return res; |
1da177e4 | 2439 | } |
1da177e4 | 2440 | |
96b8e145 HV |
2441 | if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && |
2442 | (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
2443 | fh->map_mode == ZORAN_MAP_MODE_RAW)) { | |
2444 | dprintk(1, KERN_ERR | |
2445 | "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n", | |
2446 | ZR_DEVNAME(zr)); | |
2447 | res = -EINVAL; | |
7b962d43 | 2448 | return res; |
96b8e145 | 2449 | } |
1da177e4 | 2450 | |
96b8e145 HV |
2451 | /* move into a form that we understand */ |
2452 | settings.img_x = crop->c.left; | |
2453 | settings.img_y = crop->c.top; | |
2454 | settings.img_width = crop->c.width; | |
2455 | settings.img_height = crop->c.height; | |
1da177e4 | 2456 | |
96b8e145 | 2457 | /* check validity */ |
0ba514d5 | 2458 | res = zoran_check_jpg_settings(zr, &settings, 0); |
96b8e145 | 2459 | if (res) |
7b962d43 | 2460 | return res; |
1da177e4 | 2461 | |
96b8e145 HV |
2462 | /* accept */ |
2463 | fh->jpg_settings = settings; | |
96b8e145 HV |
2464 | return res; |
2465 | } | |
1da177e4 | 2466 | |
96b8e145 HV |
2467 | static int zoran_g_jpegcomp(struct file *file, void *__fh, |
2468 | struct v4l2_jpegcompression *params) | |
2469 | { | |
2470 | struct zoran_fh *fh = __fh; | |
96b8e145 | 2471 | memset(params, 0, sizeof(*params)); |
1da177e4 | 2472 | |
96b8e145 HV |
2473 | params->quality = fh->jpg_settings.jpg_comp.quality; |
2474 | params->APPn = fh->jpg_settings.jpg_comp.APPn; | |
2475 | memcpy(params->APP_data, | |
2476 | fh->jpg_settings.jpg_comp.APP_data, | |
2477 | fh->jpg_settings.jpg_comp.APP_len); | |
2478 | params->APP_len = fh->jpg_settings.jpg_comp.APP_len; | |
2479 | memcpy(params->COM_data, | |
2480 | fh->jpg_settings.jpg_comp.COM_data, | |
2481 | fh->jpg_settings.jpg_comp.COM_len); | |
2482 | params->COM_len = fh->jpg_settings.jpg_comp.COM_len; | |
2483 | params->jpeg_markers = | |
2484 | fh->jpg_settings.jpg_comp.jpeg_markers; | |
1da177e4 | 2485 | |
96b8e145 HV |
2486 | return 0; |
2487 | } | |
1da177e4 | 2488 | |
96b8e145 | 2489 | static int zoran_s_jpegcomp(struct file *file, void *__fh, |
d88aab53 | 2490 | const struct v4l2_jpegcompression *params) |
96b8e145 HV |
2491 | { |
2492 | struct zoran_fh *fh = __fh; | |
2493 | struct zoran *zr = fh->zr; | |
2494 | int res = 0; | |
2495 | struct zoran_jpg_settings settings; | |
1da177e4 | 2496 | |
96b8e145 | 2497 | settings = fh->jpg_settings; |
1da177e4 | 2498 | |
96b8e145 | 2499 | settings.jpg_comp = *params; |
1da177e4 | 2500 | |
1159b7f1 | 2501 | if (fh->buffers.active != ZORAN_FREE) { |
96b8e145 HV |
2502 | dprintk(1, KERN_WARNING |
2503 | "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n", | |
2504 | ZR_DEVNAME(zr)); | |
2505 | res = -EBUSY; | |
7b962d43 | 2506 | return res; |
1da177e4 | 2507 | } |
1da177e4 | 2508 | |
0ba514d5 | 2509 | res = zoran_check_jpg_settings(zr, &settings, 0); |
96b8e145 | 2510 | if (res) |
7b962d43 | 2511 | return res; |
1159b7f1 TP |
2512 | if (!fh->buffers.allocated) |
2513 | fh->buffers.buffer_size = | |
2514 | zoran_v4l2_calc_bufsize(&fh->jpg_settings); | |
d88aab53 | 2515 | fh->jpg_settings.jpg_comp = settings.jpg_comp; |
27f79525 | 2516 | return res; |
1da177e4 LT |
2517 | } |
2518 | ||
1da177e4 LT |
2519 | static unsigned int |
2520 | zoran_poll (struct file *file, | |
2521 | poll_table *wait) | |
2522 | { | |
2523 | struct zoran_fh *fh = file->private_data; | |
2524 | struct zoran *zr = fh->zr; | |
e56c597b HV |
2525 | int res = v4l2_ctrl_poll(file, wait); |
2526 | int frame; | |
e42af83f | 2527 | unsigned long flags; |
1da177e4 LT |
2528 | |
2529 | /* we should check whether buffers are ready to be synced on | |
2530 | * (w/o waits - O_NONBLOCK) here | |
2531 | * if ready for read (sync), return POLLIN|POLLRDNORM, | |
2532 | * if ready for write (sync), return POLLOUT|POLLWRNORM, | |
2533 | * if error, return POLLERR, | |
2534 | * if no buffers queued or so, return POLLNVAL | |
2535 | */ | |
2536 | ||
1da177e4 LT |
2537 | switch (fh->map_mode) { |
2538 | case ZORAN_MAP_MODE_RAW: | |
e42af83f TP |
2539 | poll_wait(file, &zr->v4l_capq, wait); |
2540 | frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME]; | |
2541 | ||
2542 | spin_lock_irqsave(&zr->spinlock, flags); | |
2543 | dprintk(3, | |
2544 | KERN_DEBUG | |
2545 | "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n", | |
7e28adb2 | 2546 | ZR_DEVNAME(zr), __func__, |
1159b7f1 | 2547 | "FAL"[fh->buffers.active], zr->v4l_sync_tail, |
e42af83f TP |
2548 | "UPMD"[zr->v4l_buffers.buffer[frame].state], |
2549 | zr->v4l_pend_tail, zr->v4l_pend_head); | |
2550 | /* Process is the one capturing? */ | |
1159b7f1 | 2551 | if (fh->buffers.active != ZORAN_FREE && |
e42af83f TP |
2552 | /* Buffer ready to DQBUF? */ |
2553 | zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE) | |
e56c597b | 2554 | res |= POLLIN | POLLRDNORM; |
e42af83f TP |
2555 | spin_unlock_irqrestore(&zr->spinlock, flags); |
2556 | ||
1da177e4 LT |
2557 | break; |
2558 | ||
2559 | case ZORAN_MAP_MODE_JPG_REC: | |
2560 | case ZORAN_MAP_MODE_JPG_PLAY: | |
e42af83f | 2561 | poll_wait(file, &zr->jpg_capq, wait); |
1da177e4 | 2562 | frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME]; |
e42af83f TP |
2563 | |
2564 | spin_lock_irqsave(&zr->spinlock, flags); | |
2565 | dprintk(3, | |
2566 | KERN_DEBUG | |
2567 | "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n", | |
7e28adb2 | 2568 | ZR_DEVNAME(zr), __func__, |
1159b7f1 | 2569 | "FAL"[fh->buffers.active], zr->jpg_que_tail, |
e42af83f TP |
2570 | "UPMD"[zr->jpg_buffers.buffer[frame].state], |
2571 | zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head); | |
1159b7f1 | 2572 | if (fh->buffers.active != ZORAN_FREE && |
e42af83f | 2573 | zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) { |
1da177e4 | 2574 | if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) |
e56c597b | 2575 | res |= POLLIN | POLLRDNORM; |
1da177e4 | 2576 | else |
e56c597b | 2577 | res |= POLLOUT | POLLWRNORM; |
1da177e4 | 2578 | } |
e42af83f TP |
2579 | spin_unlock_irqrestore(&zr->spinlock, flags); |
2580 | ||
1da177e4 LT |
2581 | break; |
2582 | ||
2583 | default: | |
2584 | dprintk(1, | |
e42af83f | 2585 | KERN_ERR |
c61402ba TP |
2586 | "%s: %s - internal error, unknown map_mode=%d\n", |
2587 | ZR_DEVNAME(zr), __func__, fh->map_mode); | |
e56c597b | 2588 | res |= POLLERR; |
1da177e4 LT |
2589 | } |
2590 | ||
1da177e4 LT |
2591 | return res; |
2592 | } | |
2593 | ||
2594 | ||
2595 | /* | |
2596 | * This maps the buffers to user space. | |
2597 | * | |
2598 | * Depending on the state of fh->map_mode | |
2599 | * the V4L or the MJPEG buffers are mapped | |
2600 | * per buffer or all together | |
2601 | * | |
2602 | * Note that we need to connect to some | |
2603 | * unmap signal event to unmap the de-allocate | |
2604 | * the buffer accordingly (zoran_vm_close()) | |
2605 | */ | |
2606 | ||
2607 | static void | |
2608 | zoran_vm_open (struct vm_area_struct *vma) | |
2609 | { | |
2610 | struct zoran_mapping *map = vma->vm_private_data; | |
4ad1f70e | 2611 | atomic_inc(&map->count); |
1da177e4 LT |
2612 | } |
2613 | ||
2614 | static void | |
2615 | zoran_vm_close (struct vm_area_struct *vma) | |
2616 | { | |
2617 | struct zoran_mapping *map = vma->vm_private_data; | |
3b6456d2 | 2618 | struct zoran_fh *fh = map->fh; |
1da177e4 LT |
2619 | struct zoran *zr = fh->zr; |
2620 | int i; | |
2621 | ||
1159b7f1 TP |
2622 | dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr), |
2623 | __func__, mode_name(fh->map_mode)); | |
1da177e4 | 2624 | |
1159b7f1 TP |
2625 | for (i = 0; i < fh->buffers.num_buffers; i++) { |
2626 | if (fh->buffers.buffer[i].map == map) | |
2627 | fh->buffers.buffer[i].map = NULL; | |
2628 | } | |
2629 | kfree(map); | |
1da177e4 | 2630 | |
1159b7f1 | 2631 | /* Any buffers still mapped? */ |
4ad1f70e AV |
2632 | for (i = 0; i < fh->buffers.num_buffers; i++) { |
2633 | if (fh->buffers.buffer[i].map) { | |
1159b7f1 | 2634 | return; |
4ad1f70e AV |
2635 | } |
2636 | } | |
1da177e4 | 2637 | |
1159b7f1 TP |
2638 | dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr), |
2639 | __func__, mode_name(fh->map_mode)); | |
1da177e4 | 2640 | |
1159b7f1 TP |
2641 | if (fh->map_mode == ZORAN_MAP_MODE_RAW) { |
2642 | if (fh->buffers.active != ZORAN_FREE) { | |
2643 | unsigned long flags; | |
1da177e4 | 2644 | |
1159b7f1 TP |
2645 | spin_lock_irqsave(&zr->spinlock, flags); |
2646 | zr36057_set_memgrab(zr, 0); | |
2647 | zr->v4l_buffers.allocated = 0; | |
2648 | zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE; | |
2649 | spin_unlock_irqrestore(&zr->spinlock, flags); | |
2650 | } | |
e5ee3f64 | 2651 | v4l_fbuffer_free(fh); |
1159b7f1 TP |
2652 | } else { |
2653 | if (fh->buffers.active != ZORAN_FREE) { | |
e5ee3f64 | 2654 | jpg_qbuf(fh, -1, zr->codec_mode); |
1159b7f1 TP |
2655 | zr->jpg_buffers.allocated = 0; |
2656 | zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE; | |
1da177e4 | 2657 | } |
e5ee3f64 | 2658 | jpg_fbuffer_free(fh); |
1da177e4 LT |
2659 | } |
2660 | } | |
2661 | ||
f0f37e2f | 2662 | static const struct vm_operations_struct zoran_vm_ops = { |
1da177e4 LT |
2663 | .open = zoran_vm_open, |
2664 | .close = zoran_vm_close, | |
2665 | }; | |
2666 | ||
2667 | static int | |
2668 | zoran_mmap (struct file *file, | |
2669 | struct vm_area_struct *vma) | |
2670 | { | |
2671 | struct zoran_fh *fh = file->private_data; | |
2672 | struct zoran *zr = fh->zr; | |
2673 | unsigned long size = (vma->vm_end - vma->vm_start); | |
2674 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; | |
2675 | int i, j; | |
2676 | unsigned long page, start = vma->vm_start, todo, pos, fraglen; | |
2677 | int first, last; | |
2678 | struct zoran_mapping *map; | |
2679 | int res = 0; | |
2680 | ||
2681 | dprintk(3, | |
c61402ba TP |
2682 | KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n", |
2683 | ZR_DEVNAME(zr), __func__, | |
1159b7f1 | 2684 | mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size); |
1da177e4 LT |
2685 | |
2686 | if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) || | |
2687 | !(vma->vm_flags & VM_WRITE)) { | |
2688 | dprintk(1, | |
2689 | KERN_ERR | |
c61402ba TP |
2690 | "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n", |
2691 | ZR_DEVNAME(zr), __func__); | |
1da177e4 LT |
2692 | return -EINVAL; |
2693 | } | |
2694 | ||
1159b7f1 TP |
2695 | if (!fh->buffers.allocated) { |
2696 | dprintk(1, | |
2697 | KERN_ERR | |
c61402ba TP |
2698 | "%s: %s(%s) - buffers not yet allocated\n", |
2699 | ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode)); | |
1159b7f1 | 2700 | res = -ENOMEM; |
7b962d43 | 2701 | return res; |
1159b7f1 | 2702 | } |
1da177e4 | 2703 | |
1159b7f1 TP |
2704 | first = offset / fh->buffers.buffer_size; |
2705 | last = first - 1 + size / fh->buffers.buffer_size; | |
2706 | if (offset % fh->buffers.buffer_size != 0 || | |
2707 | size % fh->buffers.buffer_size != 0 || first < 0 || | |
2708 | last < 0 || first >= fh->buffers.num_buffers || | |
2709 | last >= fh->buffers.buffer_size) { | |
2710 | dprintk(1, | |
2711 | KERN_ERR | |
c61402ba TP |
2712 | "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n", |
2713 | ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size, | |
1159b7f1 TP |
2714 | fh->buffers.buffer_size, |
2715 | fh->buffers.num_buffers); | |
2716 | res = -EINVAL; | |
7b962d43 | 2717 | return res; |
1159b7f1 | 2718 | } |
1da177e4 | 2719 | |
1159b7f1 TP |
2720 | /* Check if any buffers are already mapped */ |
2721 | for (i = first; i <= last; i++) { | |
2722 | if (fh->buffers.buffer[i].map) { | |
1da177e4 LT |
2723 | dprintk(1, |
2724 | KERN_ERR | |
c61402ba TP |
2725 | "%s: %s(%s) - buffer %d already mapped\n", |
2726 | ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i); | |
1159b7f1 | 2727 | res = -EBUSY; |
7b962d43 | 2728 | return res; |
1da177e4 | 2729 | } |
1159b7f1 | 2730 | } |
1da177e4 | 2731 | |
1159b7f1 TP |
2732 | /* map these buffers */ |
2733 | map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL); | |
2734 | if (!map) { | |
2735 | res = -ENOMEM; | |
7b962d43 | 2736 | return res; |
1159b7f1 | 2737 | } |
3b6456d2 | 2738 | map->fh = fh; |
4ad1f70e | 2739 | atomic_set(&map->count, 1); |
1159b7f1 TP |
2740 | |
2741 | vma->vm_ops = &zoran_vm_ops; | |
2742 | vma->vm_flags |= VM_DONTEXPAND; | |
2743 | vma->vm_private_data = map; | |
2744 | ||
2745 | if (fh->map_mode == ZORAN_MAP_MODE_RAW) { | |
1da177e4 | 2746 | for (i = first; i <= last; i++) { |
1159b7f1 TP |
2747 | todo = size; |
2748 | if (todo > fh->buffers.buffer_size) | |
2749 | todo = fh->buffers.buffer_size; | |
2750 | page = fh->buffers.buffer[i].v4l.fbuffer_phys; | |
2751 | if (remap_pfn_range(vma, start, page >> PAGE_SHIFT, | |
2752 | todo, PAGE_SHARED)) { | |
1da177e4 LT |
2753 | dprintk(1, |
2754 | KERN_ERR | |
c61402ba TP |
2755 | "%s: %s(V4L) - remap_pfn_range failed\n", |
2756 | ZR_DEVNAME(zr), __func__); | |
1159b7f1 | 2757 | res = -EAGAIN; |
7b962d43 | 2758 | return res; |
1da177e4 | 2759 | } |
1159b7f1 TP |
2760 | size -= todo; |
2761 | start += todo; | |
2762 | fh->buffers.buffer[i].map = map; | |
2763 | if (size == 0) | |
2764 | break; | |
1da177e4 | 2765 | } |
1159b7f1 | 2766 | } else { |
1da177e4 LT |
2767 | for (i = first; i <= last; i++) { |
2768 | for (j = 0; | |
1159b7f1 | 2769 | j < fh->buffers.buffer_size / PAGE_SIZE; |
1da177e4 LT |
2770 | j++) { |
2771 | fraglen = | |
1159b7f1 | 2772 | (le32_to_cpu(fh->buffers.buffer[i].jpg. |
1da177e4 LT |
2773 | frag_tab[2 * j + 1]) & ~1) << 1; |
2774 | todo = size; | |
2775 | if (todo > fraglen) | |
2776 | todo = fraglen; | |
2777 | pos = | |
1159b7f1 TP |
2778 | le32_to_cpu(fh->buffers. |
2779 | buffer[i].jpg.frag_tab[2 * j]); | |
1da177e4 LT |
2780 | /* should just be pos on i386 */ |
2781 | page = virt_to_phys(bus_to_virt(pos)) | |
2782 | >> PAGE_SHIFT; | |
2783 | if (remap_pfn_range(vma, start, page, | |
2784 | todo, PAGE_SHARED)) { | |
2785 | dprintk(1, | |
2786 | KERN_ERR | |
c61402ba TP |
2787 | "%s: %s(V4L) - remap_pfn_range failed\n", |
2788 | ZR_DEVNAME(zr), __func__); | |
1da177e4 | 2789 | res = -EAGAIN; |
7b962d43 | 2790 | return res; |
1da177e4 LT |
2791 | } |
2792 | size -= todo; | |
2793 | start += todo; | |
2794 | if (size == 0) | |
2795 | break; | |
1159b7f1 | 2796 | if (le32_to_cpu(fh->buffers.buffer[i].jpg. |
1da177e4 LT |
2797 | frag_tab[2 * j + 1]) & 1) |
2798 | break; /* was last fragment */ | |
2799 | } | |
1159b7f1 | 2800 | fh->buffers.buffer[i].map = map; |
1da177e4 LT |
2801 | if (size == 0) |
2802 | break; | |
2803 | ||
2804 | } | |
1da177e4 | 2805 | } |
46a7a3cc | 2806 | return res; |
1da177e4 LT |
2807 | } |
2808 | ||
96b8e145 HV |
2809 | static const struct v4l2_ioctl_ops zoran_ioctl_ops = { |
2810 | .vidioc_querycap = zoran_querycap, | |
2811 | .vidioc_cropcap = zoran_cropcap, | |
2812 | .vidioc_s_crop = zoran_s_crop, | |
2813 | .vidioc_g_crop = zoran_g_crop, | |
2814 | .vidioc_enum_input = zoran_enum_input, | |
2815 | .vidioc_g_input = zoran_g_input, | |
2816 | .vidioc_s_input = zoran_s_input, | |
2817 | .vidioc_enum_output = zoran_enum_output, | |
2818 | .vidioc_g_output = zoran_g_output, | |
2819 | .vidioc_s_output = zoran_s_output, | |
2820 | .vidioc_g_fbuf = zoran_g_fbuf, | |
2821 | .vidioc_s_fbuf = zoran_s_fbuf, | |
2822 | .vidioc_g_std = zoran_g_std, | |
2823 | .vidioc_s_std = zoran_s_std, | |
2824 | .vidioc_g_jpegcomp = zoran_g_jpegcomp, | |
2825 | .vidioc_s_jpegcomp = zoran_s_jpegcomp, | |
2826 | .vidioc_overlay = zoran_overlay, | |
2827 | .vidioc_reqbufs = zoran_reqbufs, | |
2828 | .vidioc_querybuf = zoran_querybuf, | |
2829 | .vidioc_qbuf = zoran_qbuf, | |
2830 | .vidioc_dqbuf = zoran_dqbuf, | |
2831 | .vidioc_streamon = zoran_streamon, | |
2832 | .vidioc_streamoff = zoran_streamoff, | |
2833 | .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap, | |
2834 | .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out, | |
2835 | .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay, | |
2836 | .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap, | |
2837 | .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out, | |
2838 | .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay, | |
2839 | .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap, | |
2840 | .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out, | |
2841 | .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay, | |
2842 | .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap, | |
2843 | .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out, | |
2844 | .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay, | |
e56c597b HV |
2845 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
2846 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
96b8e145 HV |
2847 | }; |
2848 | ||
bec43661 | 2849 | static const struct v4l2_file_operations zoran_fops = { |
1da177e4 LT |
2850 | .owner = THIS_MODULE, |
2851 | .open = zoran_open, | |
2852 | .release = zoran_close, | |
7b962d43 | 2853 | .unlocked_ioctl = video_ioctl2, |
1da177e4 LT |
2854 | .mmap = zoran_mmap, |
2855 | .poll = zoran_poll, | |
2856 | }; | |
2857 | ||
4c62e976 | 2858 | struct video_device zoran_template = { |
1da177e4 | 2859 | .name = ZORAN_NAME, |
1da177e4 | 2860 | .fops = &zoran_fops, |
96b8e145 | 2861 | .ioctl_ops = &zoran_ioctl_ops, |
1da177e4 | 2862 | .release = &zoran_vdev_release, |
96b8e145 | 2863 | .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM, |
1da177e4 LT |
2864 | }; |
2865 |