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