RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / vino.c
blob1736fd6e542533ac4042099804bf350092c03579
1 /*
2 * Driver for the VINO (Video In No Out) system found in SGI Indys.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License version 2 as published by the Free Software Foundation.
7 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
9 * Based on the previous version of the driver for 2.4 kernels by:
10 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
12 * v4l2_device/v4l2_subdev conversion by:
13 * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
15 * Note: this conversion is untested! Please contact the linux-media
16 * mailinglist if you can test this, together with the test results.
20 * TODO:
21 * - remove "mark pages reserved-hacks" from memory allocation code
22 * and implement fault()
23 * - check decimation, calculating and reporting image size when
24 * using decimation
25 * - implement read(), user mode buffers and overlay (?)
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/errno.h>
33 #include <linux/fs.h>
34 #include <linux/interrupt.h>
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/mm.h>
38 #include <linux/time.h>
39 #include <linux/version.h>
40 #include <linux/kmod.h>
42 #include <linux/i2c.h>
44 #include <linux/videodev2.h>
45 #include <media/v4l2-device.h>
46 #include <media/v4l2-ioctl.h>
47 #include <linux/mutex.h>
49 #include <asm/paccess.h>
50 #include <asm/io.h>
51 #include <asm/sgi/ip22.h>
52 #include <asm/sgi/mc.h>
54 #include "vino.h"
55 #include "saa7191.h"
56 #include "indycam.h"
58 /* Uncomment the following line to get lots and lots of (mostly useless)
59 * debug info.
60 * Note that the debug output also slows down the driver significantly */
61 // #define VINO_DEBUG
62 // #define VINO_DEBUG_INT
64 #define VINO_MODULE_VERSION "0.0.6"
65 #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 6)
67 MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
68 MODULE_VERSION(VINO_MODULE_VERSION);
69 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
70 MODULE_LICENSE("GPL");
72 #ifdef VINO_DEBUG
73 #define dprintk(x...) printk("VINO: " x);
74 #else
75 #define dprintk(x...)
76 #endif
78 #define VINO_NO_CHANNEL 0
79 #define VINO_CHANNEL_A 1
80 #define VINO_CHANNEL_B 2
82 #define VINO_PAL_WIDTH 768
83 #define VINO_PAL_HEIGHT 576
84 #define VINO_NTSC_WIDTH 640
85 #define VINO_NTSC_HEIGHT 480
87 #define VINO_MIN_WIDTH 32
88 #define VINO_MIN_HEIGHT 32
90 #define VINO_CLIPPING_START_ODD_D1 1
91 #define VINO_CLIPPING_START_ODD_PAL 15
92 #define VINO_CLIPPING_START_ODD_NTSC 12
94 #define VINO_CLIPPING_START_EVEN_D1 2
95 #define VINO_CLIPPING_START_EVEN_PAL 15
96 #define VINO_CLIPPING_START_EVEN_NTSC 12
98 #define VINO_INPUT_CHANNEL_COUNT 3
100 /* the number is the index for vino_inputs */
101 #define VINO_INPUT_NONE -1
102 #define VINO_INPUT_COMPOSITE 0
103 #define VINO_INPUT_SVIDEO 1
104 #define VINO_INPUT_D1 2
106 #define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
108 #define VINO_FIFO_THRESHOLD_DEFAULT 16
110 #define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
111 * VINO_PAL_HEIGHT * 4 \
112 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
114 #define VINO_FRAMEBUFFER_COUNT_MAX 8
116 #define VINO_FRAMEBUFFER_UNUSED 0
117 #define VINO_FRAMEBUFFER_IN_USE 1
118 #define VINO_FRAMEBUFFER_READY 2
120 #define VINO_QUEUE_ERROR -1
121 #define VINO_QUEUE_MAGIC 0x20050125
123 #define VINO_MEMORY_NONE 0
124 #define VINO_MEMORY_MMAP 1
125 #define VINO_MEMORY_USERPTR 2
127 #define VINO_DUMMY_DESC_COUNT 4
128 #define VINO_DESC_FETCH_DELAY 5 /* microseconds */
130 #define VINO_MAX_FRAME_SKIP_COUNT 128
132 /* the number is the index for vino_data_formats */
133 #define VINO_DATA_FMT_NONE -1
134 #define VINO_DATA_FMT_GREY 0
135 #define VINO_DATA_FMT_RGB332 1
136 #define VINO_DATA_FMT_RGB32 2
137 #define VINO_DATA_FMT_YUV 3
139 #define VINO_DATA_FMT_COUNT 4
141 /* the number is the index for vino_data_norms */
142 #define VINO_DATA_NORM_NONE -1
143 #define VINO_DATA_NORM_NTSC 0
144 #define VINO_DATA_NORM_PAL 1
145 #define VINO_DATA_NORM_SECAM 2
146 #define VINO_DATA_NORM_D1 3
148 #define VINO_DATA_NORM_COUNT 4
150 /* I2C controller flags */
151 #define SGI_I2C_FORCE_IDLE (0 << 0)
152 #define SGI_I2C_NOT_IDLE (1 << 0)
153 #define SGI_I2C_WRITE (0 << 1)
154 #define SGI_I2C_READ (1 << 1)
155 #define SGI_I2C_RELEASE_BUS (0 << 2)
156 #define SGI_I2C_HOLD_BUS (1 << 2)
157 #define SGI_I2C_XFER_DONE (0 << 4)
158 #define SGI_I2C_XFER_BUSY (1 << 4)
159 #define SGI_I2C_ACK (0 << 5)
160 #define SGI_I2C_NACK (1 << 5)
161 #define SGI_I2C_BUS_OK (0 << 7)
162 #define SGI_I2C_BUS_ERR (1 << 7)
164 /* Internal data structure definitions */
166 struct vino_input {
167 char *name;
168 v4l2_std_id std;
171 struct vino_clipping {
172 unsigned int left, right, top, bottom;
175 struct vino_data_format {
176 /* the description */
177 char *description;
178 /* bytes per pixel */
179 unsigned int bpp;
180 /* V4L2 fourcc code */
181 __u32 pixelformat;
182 /* V4L2 colorspace (duh!) */
183 enum v4l2_colorspace colorspace;
186 struct vino_data_norm {
187 char *description;
188 unsigned int width, height;
189 struct vino_clipping odd;
190 struct vino_clipping even;
192 v4l2_std_id std;
193 unsigned int fps_min, fps_max;
194 __u32 framelines;
197 struct vino_descriptor_table {
198 /* the number of PAGE_SIZE sized pages in the buffer */
199 unsigned int page_count;
200 /* virtual (kmalloc'd) pointers to the actual data
201 * (in PAGE_SIZE chunks, used with mmap streaming) */
202 unsigned long *virtual;
204 /* cpu address for the VINO descriptor table
205 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
206 unsigned long *dma_cpu;
207 /* dma address for the VINO descriptor table
208 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
209 dma_addr_t dma;
212 struct vino_framebuffer {
213 /* identifier nubmer */
214 unsigned int id;
215 /* the length of the whole buffer */
216 unsigned int size;
217 /* the length of actual data in buffer */
218 unsigned int data_size;
219 /* the data format */
220 unsigned int data_format;
221 /* the state of buffer data */
222 unsigned int state;
223 /* is the buffer mapped in user space? */
224 unsigned int map_count;
225 /* memory offset for mmap() */
226 unsigned int offset;
227 /* frame counter */
228 unsigned int frame_counter;
229 /* timestamp (written when image capture finishes) */
230 struct timeval timestamp;
232 struct vino_descriptor_table desc_table;
234 spinlock_t state_lock;
237 struct vino_framebuffer_fifo {
238 unsigned int length;
240 unsigned int used;
241 unsigned int head;
242 unsigned int tail;
244 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
247 struct vino_framebuffer_queue {
248 unsigned int magic;
250 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
251 unsigned int type;
252 unsigned int length;
254 /* data field of in and out contain index numbers for buffer */
255 struct vino_framebuffer_fifo in;
256 struct vino_framebuffer_fifo out;
258 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
260 spinlock_t queue_lock;
261 struct mutex queue_mutex;
262 wait_queue_head_t frame_wait_queue;
265 struct vino_interrupt_data {
266 struct timeval timestamp;
267 unsigned int frame_counter;
268 unsigned int skip_count;
269 unsigned int skip;
272 struct vino_channel_settings {
273 unsigned int channel;
275 int input;
276 unsigned int data_format;
277 unsigned int data_norm;
278 struct vino_clipping clipping;
279 unsigned int decimation;
280 unsigned int line_size;
281 unsigned int alpha;
282 unsigned int fps;
283 unsigned int framert_reg;
285 unsigned int fifo_threshold;
287 struct vino_framebuffer_queue fb_queue;
289 /* number of the current field */
290 unsigned int field;
292 /* read in progress */
293 int reading;
294 /* streaming is active */
295 int streaming;
296 /* the driver is currently processing the queue */
297 int capturing;
299 struct mutex mutex;
300 spinlock_t capture_lock;
302 unsigned int users;
304 struct vino_interrupt_data int_data;
306 /* V4L support */
307 struct video_device *vdev;
310 struct vino_settings {
311 struct v4l2_device v4l2_dev;
312 struct vino_channel_settings a;
313 struct vino_channel_settings b;
315 /* the channel which owns this client:
316 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
317 unsigned int decoder_owner;
318 struct v4l2_subdev *decoder;
319 unsigned int camera_owner;
320 struct v4l2_subdev *camera;
322 /* a lock for vino register access */
323 spinlock_t vino_lock;
324 /* a lock for channel input changes */
325 spinlock_t input_lock;
327 unsigned long dummy_page;
328 struct vino_descriptor_table dummy_desc_table;
331 /* Module parameters */
334 * Using vino_pixel_conversion the ABGR32-format pixels supplied
335 * by the VINO chip can be converted to more common formats
336 * like RGBA32 (or probably RGB24 in the future). This way we
337 * can give out data that can be specified correctly with
338 * the V4L2-definitions.
340 * The pixel format is specified as RGBA32 when no conversion
341 * is used.
343 * Note that this only affects the 32-bit bit depth.
345 * Use non-zero value to enable conversion.
347 static int vino_pixel_conversion;
349 module_param_named(pixelconv, vino_pixel_conversion, int, 0);
351 MODULE_PARM_DESC(pixelconv,
352 "enable pixel conversion (non-zero value enables)");
354 /* Internal data structures */
356 static struct sgi_vino *vino;
358 static struct vino_settings *vino_drvdata;
360 #define camera_call(o, f, args...) \
361 v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
362 #define decoder_call(o, f, args...) \
363 v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
365 static const char *vino_driver_name = "vino";
366 static const char *vino_driver_description = "SGI VINO";
367 static const char *vino_bus_name = "GIO64 bus";
368 static const char *vino_vdev_name_a = "SGI VINO Channel A";
369 static const char *vino_vdev_name_b = "SGI VINO Channel B";
371 static void vino_capture_tasklet(unsigned long channel);
373 DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
374 DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
376 static const struct vino_input vino_inputs[] = {
378 .name = "Composite",
379 .std = V4L2_STD_NTSC | V4L2_STD_PAL
380 | V4L2_STD_SECAM,
381 }, {
382 .name = "S-Video",
383 .std = V4L2_STD_NTSC | V4L2_STD_PAL
384 | V4L2_STD_SECAM,
385 }, {
386 .name = "D1/IndyCam",
387 .std = V4L2_STD_NTSC,
391 static const struct vino_data_format vino_data_formats[] = {
393 .description = "8-bit greyscale",
394 .bpp = 1,
395 .pixelformat = V4L2_PIX_FMT_GREY,
396 .colorspace = V4L2_COLORSPACE_SMPTE170M,
397 }, {
398 .description = "8-bit dithered RGB 3-3-2",
399 .bpp = 1,
400 .pixelformat = V4L2_PIX_FMT_RGB332,
401 .colorspace = V4L2_COLORSPACE_SRGB,
402 }, {
403 .description = "32-bit RGB",
404 .bpp = 4,
405 .pixelformat = V4L2_PIX_FMT_RGB32,
406 .colorspace = V4L2_COLORSPACE_SRGB,
407 }, {
408 .description = "YUV 4:2:2",
409 .bpp = 2,
410 .pixelformat = V4L2_PIX_FMT_YUYV,
411 .colorspace = V4L2_COLORSPACE_SMPTE170M,
415 static const struct vino_data_norm vino_data_norms[] = {
417 .description = "NTSC",
418 .std = V4L2_STD_NTSC,
419 .fps_min = 6,
420 .fps_max = 30,
421 .framelines = 525,
422 .width = VINO_NTSC_WIDTH,
423 .height = VINO_NTSC_HEIGHT,
424 .odd = {
425 .top = VINO_CLIPPING_START_ODD_NTSC,
426 .left = 0,
427 .bottom = VINO_CLIPPING_START_ODD_NTSC
428 + VINO_NTSC_HEIGHT / 2 - 1,
429 .right = VINO_NTSC_WIDTH,
431 .even = {
432 .top = VINO_CLIPPING_START_EVEN_NTSC,
433 .left = 0,
434 .bottom = VINO_CLIPPING_START_EVEN_NTSC
435 + VINO_NTSC_HEIGHT / 2 - 1,
436 .right = VINO_NTSC_WIDTH,
438 }, {
439 .description = "PAL",
440 .std = V4L2_STD_PAL,
441 .fps_min = 5,
442 .fps_max = 25,
443 .framelines = 625,
444 .width = VINO_PAL_WIDTH,
445 .height = VINO_PAL_HEIGHT,
446 .odd = {
447 .top = VINO_CLIPPING_START_ODD_PAL,
448 .left = 0,
449 .bottom = VINO_CLIPPING_START_ODD_PAL
450 + VINO_PAL_HEIGHT / 2 - 1,
451 .right = VINO_PAL_WIDTH,
453 .even = {
454 .top = VINO_CLIPPING_START_EVEN_PAL,
455 .left = 0,
456 .bottom = VINO_CLIPPING_START_EVEN_PAL
457 + VINO_PAL_HEIGHT / 2 - 1,
458 .right = VINO_PAL_WIDTH,
460 }, {
461 .description = "SECAM",
462 .std = V4L2_STD_SECAM,
463 .fps_min = 5,
464 .fps_max = 25,
465 .framelines = 625,
466 .width = VINO_PAL_WIDTH,
467 .height = VINO_PAL_HEIGHT,
468 .odd = {
469 .top = VINO_CLIPPING_START_ODD_PAL,
470 .left = 0,
471 .bottom = VINO_CLIPPING_START_ODD_PAL
472 + VINO_PAL_HEIGHT / 2 - 1,
473 .right = VINO_PAL_WIDTH,
475 .even = {
476 .top = VINO_CLIPPING_START_EVEN_PAL,
477 .left = 0,
478 .bottom = VINO_CLIPPING_START_EVEN_PAL
479 + VINO_PAL_HEIGHT / 2 - 1,
480 .right = VINO_PAL_WIDTH,
482 }, {
483 .description = "NTSC/D1",
484 .std = V4L2_STD_NTSC,
485 .fps_min = 6,
486 .fps_max = 30,
487 .framelines = 525,
488 .width = VINO_NTSC_WIDTH,
489 .height = VINO_NTSC_HEIGHT,
490 .odd = {
491 .top = VINO_CLIPPING_START_ODD_D1,
492 .left = 0,
493 .bottom = VINO_CLIPPING_START_ODD_D1
494 + VINO_NTSC_HEIGHT / 2 - 1,
495 .right = VINO_NTSC_WIDTH,
497 .even = {
498 .top = VINO_CLIPPING_START_EVEN_D1,
499 .left = 0,
500 .bottom = VINO_CLIPPING_START_EVEN_D1
501 + VINO_NTSC_HEIGHT / 2 - 1,
502 .right = VINO_NTSC_WIDTH,
507 #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
509 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
511 .id = V4L2_CID_AUTOGAIN,
512 .type = V4L2_CTRL_TYPE_BOOLEAN,
513 .name = "Automatic Gain Control",
514 .minimum = 0,
515 .maximum = 1,
516 .step = 1,
517 .default_value = INDYCAM_AGC_DEFAULT,
518 }, {
519 .id = V4L2_CID_AUTO_WHITE_BALANCE,
520 .type = V4L2_CTRL_TYPE_BOOLEAN,
521 .name = "Automatic White Balance",
522 .minimum = 0,
523 .maximum = 1,
524 .step = 1,
525 .default_value = INDYCAM_AWB_DEFAULT,
526 }, {
527 .id = V4L2_CID_GAIN,
528 .type = V4L2_CTRL_TYPE_INTEGER,
529 .name = "Gain",
530 .minimum = INDYCAM_GAIN_MIN,
531 .maximum = INDYCAM_GAIN_MAX,
532 .step = 1,
533 .default_value = INDYCAM_GAIN_DEFAULT,
534 }, {
535 .id = INDYCAM_CONTROL_RED_SATURATION,
536 .type = V4L2_CTRL_TYPE_INTEGER,
537 .name = "Red Saturation",
538 .minimum = INDYCAM_RED_SATURATION_MIN,
539 .maximum = INDYCAM_RED_SATURATION_MAX,
540 .step = 1,
541 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
542 }, {
543 .id = INDYCAM_CONTROL_BLUE_SATURATION,
544 .type = V4L2_CTRL_TYPE_INTEGER,
545 .name = "Blue Saturation",
546 .minimum = INDYCAM_BLUE_SATURATION_MIN,
547 .maximum = INDYCAM_BLUE_SATURATION_MAX,
548 .step = 1,
549 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
550 }, {
551 .id = V4L2_CID_RED_BALANCE,
552 .type = V4L2_CTRL_TYPE_INTEGER,
553 .name = "Red Balance",
554 .minimum = INDYCAM_RED_BALANCE_MIN,
555 .maximum = INDYCAM_RED_BALANCE_MAX,
556 .step = 1,
557 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
558 }, {
559 .id = V4L2_CID_BLUE_BALANCE,
560 .type = V4L2_CTRL_TYPE_INTEGER,
561 .name = "Blue Balance",
562 .minimum = INDYCAM_BLUE_BALANCE_MIN,
563 .maximum = INDYCAM_BLUE_BALANCE_MAX,
564 .step = 1,
565 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
566 }, {
567 .id = V4L2_CID_EXPOSURE,
568 .type = V4L2_CTRL_TYPE_INTEGER,
569 .name = "Shutter Control",
570 .minimum = INDYCAM_SHUTTER_MIN,
571 .maximum = INDYCAM_SHUTTER_MAX,
572 .step = 1,
573 .default_value = INDYCAM_SHUTTER_DEFAULT,
574 }, {
575 .id = V4L2_CID_GAMMA,
576 .type = V4L2_CTRL_TYPE_INTEGER,
577 .name = "Gamma",
578 .minimum = INDYCAM_GAMMA_MIN,
579 .maximum = INDYCAM_GAMMA_MAX,
580 .step = 1,
581 .default_value = INDYCAM_GAMMA_DEFAULT,
585 #define VINO_SAA7191_V4L2_CONTROL_COUNT 9
587 struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
589 .id = V4L2_CID_HUE,
590 .type = V4L2_CTRL_TYPE_INTEGER,
591 .name = "Hue",
592 .minimum = SAA7191_HUE_MIN,
593 .maximum = SAA7191_HUE_MAX,
594 .step = 1,
595 .default_value = SAA7191_HUE_DEFAULT,
596 }, {
597 .id = SAA7191_CONTROL_BANDPASS,
598 .type = V4L2_CTRL_TYPE_INTEGER,
599 .name = "Luminance Bandpass",
600 .minimum = SAA7191_BANDPASS_MIN,
601 .maximum = SAA7191_BANDPASS_MAX,
602 .step = 1,
603 .default_value = SAA7191_BANDPASS_DEFAULT,
604 }, {
605 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
606 .type = V4L2_CTRL_TYPE_INTEGER,
607 .name = "Luminance Bandpass Weight",
608 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
609 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
610 .step = 1,
611 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
612 }, {
613 .id = SAA7191_CONTROL_CORING,
614 .type = V4L2_CTRL_TYPE_INTEGER,
615 .name = "HF Luminance Coring",
616 .minimum = SAA7191_CORING_MIN,
617 .maximum = SAA7191_CORING_MAX,
618 .step = 1,
619 .default_value = SAA7191_CORING_DEFAULT,
620 }, {
621 .id = SAA7191_CONTROL_FORCE_COLOUR,
622 .type = V4L2_CTRL_TYPE_BOOLEAN,
623 .name = "Force Colour",
624 .minimum = SAA7191_FORCE_COLOUR_MIN,
625 .maximum = SAA7191_FORCE_COLOUR_MAX,
626 .step = 1,
627 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
628 }, {
629 .id = SAA7191_CONTROL_CHROMA_GAIN,
630 .type = V4L2_CTRL_TYPE_INTEGER,
631 .name = "Chrominance Gain Control",
632 .minimum = SAA7191_CHROMA_GAIN_MIN,
633 .maximum = SAA7191_CHROMA_GAIN_MAX,
634 .step = 1,
635 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
636 }, {
637 .id = SAA7191_CONTROL_VTRC,
638 .type = V4L2_CTRL_TYPE_BOOLEAN,
639 .name = "VTR Time Constant",
640 .minimum = SAA7191_VTRC_MIN,
641 .maximum = SAA7191_VTRC_MAX,
642 .step = 1,
643 .default_value = SAA7191_VTRC_DEFAULT,
644 }, {
645 .id = SAA7191_CONTROL_LUMA_DELAY,
646 .type = V4L2_CTRL_TYPE_INTEGER,
647 .name = "Luminance Delay Compensation",
648 .minimum = SAA7191_LUMA_DELAY_MIN,
649 .maximum = SAA7191_LUMA_DELAY_MAX,
650 .step = 1,
651 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
652 }, {
653 .id = SAA7191_CONTROL_VNR,
654 .type = V4L2_CTRL_TYPE_INTEGER,
655 .name = "Vertical Noise Reduction",
656 .minimum = SAA7191_VNR_MIN,
657 .maximum = SAA7191_VNR_MAX,
658 .step = 1,
659 .default_value = SAA7191_VNR_DEFAULT,
663 /* VINO framebuffer/DMA descriptor management */
665 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
666 unsigned int count)
668 unsigned int i;
670 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
672 for (i = 0; i < count; i++) {
673 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
674 dma_unmap_single(NULL,
675 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
676 PAGE_SIZE, DMA_FROM_DEVICE);
677 free_page(fb->desc_table.virtual[i]);
680 dma_free_coherent(NULL,
681 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
682 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
683 fb->desc_table.dma);
684 kfree(fb->desc_table.virtual);
686 memset(fb, 0, sizeof(struct vino_framebuffer));
689 static void vino_free_buffer(struct vino_framebuffer *fb)
691 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
694 static int vino_allocate_buffer(struct vino_framebuffer *fb,
695 unsigned int size)
697 unsigned int count, i, j;
698 int ret = 0;
700 dprintk("vino_allocate_buffer():\n");
702 if (size < 1)
703 return -EINVAL;
705 memset(fb, 0, sizeof(struct vino_framebuffer));
707 count = ((size / PAGE_SIZE) + 4) & ~3;
709 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
710 size, count);
712 /* allocate memory for table with virtual (page) addresses */
713 fb->desc_table.virtual = (unsigned long *)
714 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
715 if (!fb->desc_table.virtual)
716 return -ENOMEM;
718 /* allocate memory for table with dma addresses
719 * (has space for four extra descriptors) */
720 fb->desc_table.dma_cpu =
721 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
722 sizeof(dma_addr_t), &fb->desc_table.dma,
723 GFP_KERNEL | GFP_DMA);
724 if (!fb->desc_table.dma_cpu) {
725 ret = -ENOMEM;
726 goto out_free_virtual;
729 /* allocate pages for the buffer and acquire the according
730 * dma addresses */
731 for (i = 0; i < count; i++) {
732 dma_addr_t dma_data_addr;
734 fb->desc_table.virtual[i] =
735 get_zeroed_page(GFP_KERNEL | GFP_DMA);
736 if (!fb->desc_table.virtual[i]) {
737 ret = -ENOBUFS;
738 break;
741 dma_data_addr =
742 dma_map_single(NULL,
743 (void *)fb->desc_table.virtual[i],
744 PAGE_SIZE, DMA_FROM_DEVICE);
746 for (j = 0; j < VINO_PAGE_RATIO; j++) {
747 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
748 dma_data_addr + VINO_PAGE_SIZE * j;
751 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
754 /* page_count needs to be set anyway, because the descriptor table has
755 * been allocated according to this number */
756 fb->desc_table.page_count = count;
758 if (ret) {
759 /* the descriptor with index i doesn't contain
760 * a valid address yet */
761 vino_free_buffer_with_count(fb, i);
762 return ret;
765 //fb->size = size;
766 fb->size = count * PAGE_SIZE;
767 fb->data_format = VINO_DATA_FMT_NONE;
769 /* set the dma stop-bit for the last (count+1)th descriptor */
770 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
771 return 0;
773 out_free_virtual:
774 kfree(fb->desc_table.virtual);
775 return ret;
779 static void vino_sync_buffer(struct vino_framebuffer *fb)
781 int i;
783 dprintk("vino_sync_buffer():\n");
785 for (i = 0; i < fb->desc_table.page_count; i++)
786 dma_sync_single_for_cpu(NULL,
787 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
788 PAGE_SIZE, DMA_FROM_DEVICE);
791 /* Framebuffer fifo functions (need to be locked externally) */
793 static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
794 unsigned int length)
796 f->length = 0;
797 f->used = 0;
798 f->head = 0;
799 f->tail = 0;
801 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
802 length = VINO_FRAMEBUFFER_COUNT_MAX;
804 f->length = length;
807 /* returns true/false */
808 static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
809 unsigned int id)
811 unsigned int i;
813 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
814 if (f->data[i] == id)
815 return 1;
818 return 0;
822 static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
824 return f->used;
827 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
829 if (id >= f->length) {
830 return VINO_QUEUE_ERROR;
833 if (vino_fifo_has_id(f, id)) {
834 return VINO_QUEUE_ERROR;
837 if (f->used < f->length) {
838 f->data[f->tail] = id;
839 f->tail = (f->tail + 1) % f->length;
840 f->used++;
841 } else {
842 return VINO_QUEUE_ERROR;
845 return 0;
848 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
850 if (f->used > 0) {
851 *id = f->data[f->head];
852 } else {
853 return VINO_QUEUE_ERROR;
856 return 0;
859 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
861 if (f->used > 0) {
862 *id = f->data[f->head];
863 f->head = (f->head + 1) % f->length;
864 f->used--;
865 } else {
866 return VINO_QUEUE_ERROR;
869 return 0;
872 /* Framebuffer queue functions */
874 /* execute with queue_lock locked */
875 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
876 unsigned int length)
878 unsigned int i;
880 q->length = 0;
881 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
882 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
883 for (i = 0; i < length; i++) {
884 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
886 vino_free_buffer(q->buffer[i]);
887 kfree(q->buffer[i]);
890 q->type = VINO_MEMORY_NONE;
891 q->magic = 0;
894 static void vino_queue_free(struct vino_framebuffer_queue *q)
896 dprintk("vino_queue_free():\n");
898 if (q->magic != VINO_QUEUE_MAGIC)
899 return;
900 if (q->type != VINO_MEMORY_MMAP)
901 return;
903 mutex_lock(&q->queue_mutex);
905 vino_queue_free_with_count(q, q->length);
907 mutex_unlock(&q->queue_mutex);
910 static int vino_queue_init(struct vino_framebuffer_queue *q,
911 unsigned int *length)
913 unsigned int i;
914 int ret = 0;
916 dprintk("vino_queue_init(): length = %d\n", *length);
918 if (q->magic == VINO_QUEUE_MAGIC) {
919 dprintk("vino_queue_init(): queue already initialized!\n");
920 return -EINVAL;
923 if (q->type != VINO_MEMORY_NONE) {
924 dprintk("vino_queue_init(): queue already initialized!\n");
925 return -EINVAL;
928 if (*length < 1)
929 return -EINVAL;
931 mutex_lock(&q->queue_mutex);
933 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
934 *length = VINO_FRAMEBUFFER_COUNT_MAX;
936 q->length = 0;
938 for (i = 0; i < *length; i++) {
939 dprintk("vino_queue_init(): allocating buffer %d\n", i);
940 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
941 GFP_KERNEL);
942 if (!q->buffer[i]) {
943 dprintk("vino_queue_init(): kmalloc() failed\n");
944 ret = -ENOMEM;
945 break;
948 ret = vino_allocate_buffer(q->buffer[i],
949 VINO_FRAMEBUFFER_SIZE);
950 if (ret) {
951 kfree(q->buffer[i]);
952 dprintk("vino_queue_init(): "
953 "vino_allocate_buffer() failed\n");
954 break;
957 q->buffer[i]->id = i;
958 if (i > 0) {
959 q->buffer[i]->offset = q->buffer[i - 1]->offset +
960 q->buffer[i - 1]->size;
961 } else {
962 q->buffer[i]->offset = 0;
965 spin_lock_init(&q->buffer[i]->state_lock);
967 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
968 "size = %d\n", i, q->buffer[i]->offset,
969 q->buffer[i]->size);
972 if (ret) {
973 vino_queue_free_with_count(q, i);
974 *length = 0;
975 } else {
976 q->length = *length;
977 vino_fifo_init(&q->in, q->length);
978 vino_fifo_init(&q->out, q->length);
979 q->type = VINO_MEMORY_MMAP;
980 q->magic = VINO_QUEUE_MAGIC;
983 mutex_unlock(&q->queue_mutex);
985 return ret;
988 static struct vino_framebuffer *vino_queue_add(struct
989 vino_framebuffer_queue *q,
990 unsigned int id)
992 struct vino_framebuffer *ret = NULL;
993 unsigned int total;
994 unsigned long flags;
996 dprintk("vino_queue_add(): id = %d\n", id);
998 if (q->magic != VINO_QUEUE_MAGIC) {
999 return ret;
1002 spin_lock_irqsave(&q->queue_lock, flags);
1004 if (q->length == 0)
1005 goto out;
1007 if (id >= q->length)
1008 goto out;
1010 /* not needed?: if (vino_fifo_full(&q->out)) {
1011 goto out;
1013 /* check that outgoing queue isn't already full
1014 * (or that it won't become full) */
1015 total = vino_fifo_get_used(&q->in) +
1016 vino_fifo_get_used(&q->out);
1017 if (total >= q->length)
1018 goto out;
1020 if (vino_fifo_enqueue(&q->in, id))
1021 goto out;
1023 ret = q->buffer[id];
1025 out:
1026 spin_unlock_irqrestore(&q->queue_lock, flags);
1028 return ret;
1031 static struct vino_framebuffer *vino_queue_transfer(struct
1032 vino_framebuffer_queue *q)
1034 struct vino_framebuffer *ret = NULL;
1035 struct vino_framebuffer *fb;
1036 int id;
1037 unsigned long flags;
1039 dprintk("vino_queue_transfer():\n");
1041 if (q->magic != VINO_QUEUE_MAGIC) {
1042 return ret;
1045 spin_lock_irqsave(&q->queue_lock, flags);
1047 if (q->length == 0)
1048 goto out;
1050 // now this actually removes an entry from the incoming queue
1051 if (vino_fifo_dequeue(&q->in, &id)) {
1052 goto out;
1055 dprintk("vino_queue_transfer(): id = %d\n", id);
1056 fb = q->buffer[id];
1058 // we have already checked that the outgoing queue is not full, but...
1059 if (vino_fifo_enqueue(&q->out, id)) {
1060 printk(KERN_ERR "vino_queue_transfer(): "
1061 "outgoing queue is full, this shouldn't happen!\n");
1062 goto out;
1065 ret = fb;
1066 out:
1067 spin_unlock_irqrestore(&q->queue_lock, flags);
1069 return ret;
1072 /* returns true/false */
1073 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1074 unsigned int id)
1076 int ret = 0;
1077 unsigned long flags;
1079 if (q->magic != VINO_QUEUE_MAGIC) {
1080 return ret;
1083 spin_lock_irqsave(&q->queue_lock, flags);
1085 if (q->length == 0)
1086 goto out;
1088 ret = vino_fifo_has_id(&q->in, id);
1090 out:
1091 spin_unlock_irqrestore(&q->queue_lock, flags);
1093 return ret;
1096 /* returns true/false */
1097 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1098 unsigned int id)
1100 int ret = 0;
1101 unsigned long flags;
1103 if (q->magic != VINO_QUEUE_MAGIC) {
1104 return ret;
1107 spin_lock_irqsave(&q->queue_lock, flags);
1109 if (q->length == 0)
1110 goto out;
1112 ret = vino_fifo_has_id(&q->out, id);
1114 out:
1115 spin_unlock_irqrestore(&q->queue_lock, flags);
1117 return ret;
1120 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1121 unsigned int *used)
1123 int ret = 0;
1124 unsigned long flags;
1126 if (q->magic != VINO_QUEUE_MAGIC) {
1127 return VINO_QUEUE_ERROR;
1130 spin_lock_irqsave(&q->queue_lock, flags);
1132 if (q->length == 0) {
1133 ret = VINO_QUEUE_ERROR;
1134 goto out;
1137 *used = vino_fifo_get_used(&q->in);
1139 out:
1140 spin_unlock_irqrestore(&q->queue_lock, flags);
1142 return ret;
1145 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1146 unsigned int *used)
1148 int ret = 0;
1149 unsigned long flags;
1151 if (q->magic != VINO_QUEUE_MAGIC) {
1152 return VINO_QUEUE_ERROR;
1155 spin_lock_irqsave(&q->queue_lock, flags);
1157 if (q->length == 0) {
1158 ret = VINO_QUEUE_ERROR;
1159 goto out;
1162 *used = vino_fifo_get_used(&q->out);
1164 out:
1165 spin_unlock_irqrestore(&q->queue_lock, flags);
1167 return ret;
1171 static struct vino_framebuffer *vino_queue_peek(struct
1172 vino_framebuffer_queue *q,
1173 unsigned int *id)
1175 struct vino_framebuffer *ret = NULL;
1176 unsigned long flags;
1178 if (q->magic != VINO_QUEUE_MAGIC) {
1179 return ret;
1182 spin_lock_irqsave(&q->queue_lock, flags);
1184 if (q->length == 0)
1185 goto out;
1187 if (vino_fifo_peek(&q->in, id)) {
1188 goto out;
1191 ret = q->buffer[*id];
1192 out:
1193 spin_unlock_irqrestore(&q->queue_lock, flags);
1195 return ret;
1198 static struct vino_framebuffer *vino_queue_remove(struct
1199 vino_framebuffer_queue *q,
1200 unsigned int *id)
1202 struct vino_framebuffer *ret = NULL;
1203 unsigned long flags;
1204 dprintk("vino_queue_remove():\n");
1206 if (q->magic != VINO_QUEUE_MAGIC) {
1207 return ret;
1210 spin_lock_irqsave(&q->queue_lock, flags);
1212 if (q->length == 0)
1213 goto out;
1215 if (vino_fifo_dequeue(&q->out, id)) {
1216 goto out;
1219 dprintk("vino_queue_remove(): id = %d\n", *id);
1220 ret = q->buffer[*id];
1221 out:
1222 spin_unlock_irqrestore(&q->queue_lock, flags);
1224 return ret;
1227 static struct
1228 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1229 unsigned int id)
1231 struct vino_framebuffer *ret = NULL;
1232 unsigned long flags;
1234 if (q->magic != VINO_QUEUE_MAGIC) {
1235 return ret;
1238 spin_lock_irqsave(&q->queue_lock, flags);
1240 if (q->length == 0)
1241 goto out;
1243 if (id >= q->length)
1244 goto out;
1246 ret = q->buffer[id];
1247 out:
1248 spin_unlock_irqrestore(&q->queue_lock, flags);
1250 return ret;
1253 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1255 unsigned int length = 0;
1256 unsigned long flags;
1258 if (q->magic != VINO_QUEUE_MAGIC) {
1259 return length;
1262 spin_lock_irqsave(&q->queue_lock, flags);
1263 length = q->length;
1264 spin_unlock_irqrestore(&q->queue_lock, flags);
1266 return length;
1269 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1271 unsigned int i;
1272 int ret = 0;
1273 unsigned long flags;
1275 if (q->magic != VINO_QUEUE_MAGIC) {
1276 return ret;
1279 spin_lock_irqsave(&q->queue_lock, flags);
1280 for (i = 0; i < q->length; i++) {
1281 if (q->buffer[i]->map_count > 0) {
1282 ret = 1;
1283 break;
1286 spin_unlock_irqrestore(&q->queue_lock, flags);
1288 return ret;
1291 /* VINO functions */
1293 /* execute with input_lock locked */
1294 static void vino_update_line_size(struct vino_channel_settings *vcs)
1296 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1297 unsigned int d = vcs->decimation;
1298 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1299 unsigned int lsize;
1301 dprintk("update_line_size(): before: w = %d, d = %d, "
1302 "line_size = %d\n", w, d, vcs->line_size);
1304 /* line size must be multiple of 8 bytes */
1305 lsize = (bpp * (w / d)) & ~7;
1306 w = (lsize / bpp) * d;
1308 vcs->clipping.right = vcs->clipping.left + w;
1309 vcs->line_size = lsize;
1311 dprintk("update_line_size(): after: w = %d, d = %d, "
1312 "line_size = %d\n", w, d, vcs->line_size);
1315 /* execute with input_lock locked */
1316 static void vino_set_clipping(struct vino_channel_settings *vcs,
1317 unsigned int x, unsigned int y,
1318 unsigned int w, unsigned int h)
1320 unsigned int maxwidth, maxheight;
1321 unsigned int d;
1323 maxwidth = vino_data_norms[vcs->data_norm].width;
1324 maxheight = vino_data_norms[vcs->data_norm].height;
1325 d = vcs->decimation;
1327 y &= ~1; /* odd/even fields */
1329 if (x > maxwidth) {
1330 x = 0;
1332 if (y > maxheight) {
1333 y = 0;
1336 if (((w / d) < VINO_MIN_WIDTH)
1337 || ((h / d) < VINO_MIN_HEIGHT)) {
1338 w = VINO_MIN_WIDTH * d;
1339 h = VINO_MIN_HEIGHT * d;
1342 if ((x + w) > maxwidth) {
1343 w = maxwidth - x;
1344 if ((w / d) < VINO_MIN_WIDTH)
1345 x = maxwidth - VINO_MIN_WIDTH * d;
1347 if ((y + h) > maxheight) {
1348 h = maxheight - y;
1349 if ((h / d) < VINO_MIN_HEIGHT)
1350 y = maxheight - VINO_MIN_HEIGHT * d;
1353 vcs->clipping.left = x;
1354 vcs->clipping.top = y;
1355 vcs->clipping.right = x + w;
1356 vcs->clipping.bottom = y + h;
1358 vino_update_line_size(vcs);
1360 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1361 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1362 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1365 /* execute with input_lock locked */
1366 static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
1368 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1369 vino_data_norms[vcs->data_norm].height);
1372 /* execute with input_lock locked */
1373 static void vino_set_scaling(struct vino_channel_settings *vcs,
1374 unsigned int w, unsigned int h)
1376 unsigned int x, y, curw, curh, d;
1378 x = vcs->clipping.left;
1379 y = vcs->clipping.top;
1380 curw = vcs->clipping.right - vcs->clipping.left;
1381 curh = vcs->clipping.bottom - vcs->clipping.top;
1383 d = max(curw / w, curh / h);
1385 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1386 w, h, curw, curh, d);
1388 if (d < 1) {
1389 d = 1;
1390 } else if (d > 8) {
1391 d = 8;
1394 vcs->decimation = d;
1395 vino_set_clipping(vcs, x, y, w * d, h * d);
1397 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1398 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1399 vcs->decimation, vcs->line_size);
1402 /* execute with input_lock locked */
1403 static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
1405 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1406 vcs->clipping.bottom - vcs->clipping.top);
1409 /* execute with input_lock locked */
1410 static void vino_set_framerate(struct vino_channel_settings *vcs,
1411 unsigned int fps)
1413 unsigned int mask;
1415 switch (vcs->data_norm) {
1416 case VINO_DATA_NORM_NTSC:
1417 case VINO_DATA_NORM_D1:
1418 fps = (unsigned int)(fps / 6) * 6;
1420 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1421 fps = vino_data_norms[vcs->data_norm].fps_min;
1422 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1423 fps = vino_data_norms[vcs->data_norm].fps_max;
1425 switch (fps) {
1426 case 6:
1427 mask = 0x003;
1428 break;
1429 case 12:
1430 mask = 0x0c3;
1431 break;
1432 case 18:
1433 mask = 0x333;
1434 break;
1435 case 24:
1436 mask = 0x3ff;
1437 break;
1438 case 30:
1439 mask = 0xfff;
1440 break;
1441 default:
1442 mask = VINO_FRAMERT_FULL;
1444 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1445 break;
1446 case VINO_DATA_NORM_PAL:
1447 case VINO_DATA_NORM_SECAM:
1448 fps = (unsigned int)(fps / 5) * 5;
1450 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1451 fps = vino_data_norms[vcs->data_norm].fps_min;
1452 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1453 fps = vino_data_norms[vcs->data_norm].fps_max;
1455 switch (fps) {
1456 case 5:
1457 mask = 0x003;
1458 break;
1459 case 10:
1460 mask = 0x0c3;
1461 break;
1462 case 15:
1463 mask = 0x333;
1464 break;
1465 case 20:
1466 mask = 0x0ff;
1467 break;
1468 case 25:
1469 mask = 0x3ff;
1470 break;
1471 default:
1472 mask = VINO_FRAMERT_FULL;
1474 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1475 break;
1478 vcs->fps = fps;
1481 /* execute with input_lock locked */
1482 static inline void vino_set_default_framerate(struct
1483 vino_channel_settings *vcs)
1485 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1488 /* VINO I2C bus functions */
1490 struct i2c_algo_sgi_data {
1491 void *data; /* private data for lowlevel routines */
1492 unsigned (*getctrl)(void *data);
1493 void (*setctrl)(void *data, unsigned val);
1494 unsigned (*rdata)(void *data);
1495 void (*wdata)(void *data, unsigned val);
1497 int xfer_timeout;
1498 int ack_timeout;
1501 static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1503 int i;
1505 for (i = 0; i < adap->xfer_timeout; i++) {
1506 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1507 return 0;
1508 udelay(1);
1511 return -ETIMEDOUT;
1514 static int wait_ack(struct i2c_algo_sgi_data *adap)
1516 int i;
1518 if (wait_xfer_done(adap))
1519 return -ETIMEDOUT;
1520 for (i = 0; i < adap->ack_timeout; i++) {
1521 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1522 return 0;
1523 udelay(1);
1526 return -ETIMEDOUT;
1529 static int force_idle(struct i2c_algo_sgi_data *adap)
1531 int i;
1533 adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1534 for (i = 0; i < adap->xfer_timeout; i++) {
1535 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1536 goto out;
1537 udelay(1);
1539 return -ETIMEDOUT;
1540 out:
1541 if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1542 return -EIO;
1543 return 0;
1546 static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1547 int rd)
1549 if (rd)
1550 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1551 /* Check if bus is idle, eventually force it to do so */
1552 if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1553 if (force_idle(adap))
1554 return -EIO;
1555 /* Write out the i2c chip address and specify operation */
1556 adap->setctrl(adap->data,
1557 SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1558 if (rd)
1559 addr |= 1;
1560 adap->wdata(adap->data, addr);
1561 if (wait_ack(adap))
1562 return -EIO;
1563 return 0;
1566 static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1567 unsigned int len)
1569 int i;
1571 adap->setctrl(adap->data,
1572 SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1573 for (i = 0; i < len; i++) {
1574 if (wait_xfer_done(adap))
1575 return -EIO;
1576 buf[i] = adap->rdata(adap->data);
1578 adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1580 return 0;
1584 static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1585 unsigned int len)
1587 int i;
1589 /* We are already in write state */
1590 for (i = 0; i < len; i++) {
1591 adap->wdata(adap->data, buf[i]);
1592 if (wait_ack(adap))
1593 return -EIO;
1595 return 0;
1598 static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1599 int num)
1601 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1602 struct i2c_msg *p;
1603 int i, err = 0;
1605 for (i = 0; !err && i < num; i++) {
1606 p = &msgs[i];
1607 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1608 if (err || !p->len)
1609 continue;
1610 if (p->flags & I2C_M_RD)
1611 err = i2c_read(adap, p->buf, p->len);
1612 else
1613 err = i2c_write(adap, p->buf, p->len);
1616 return (err < 0) ? err : i;
1619 static u32 sgi_func(struct i2c_adapter *adap)
1621 return I2C_FUNC_SMBUS_EMUL;
1624 static const struct i2c_algorithm sgi_algo = {
1625 .master_xfer = sgi_xfer,
1626 .functionality = sgi_func,
1629 static unsigned i2c_vino_getctrl(void *data)
1631 return vino->i2c_control;
1634 static void i2c_vino_setctrl(void *data, unsigned val)
1636 vino->i2c_control = val;
1639 static unsigned i2c_vino_rdata(void *data)
1641 return vino->i2c_data;
1644 static void i2c_vino_wdata(void *data, unsigned val)
1646 vino->i2c_data = val;
1649 static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1650 .getctrl = &i2c_vino_getctrl,
1651 .setctrl = &i2c_vino_setctrl,
1652 .rdata = &i2c_vino_rdata,
1653 .wdata = &i2c_vino_wdata,
1654 .xfer_timeout = 200,
1655 .ack_timeout = 1000,
1658 static struct i2c_adapter vino_i2c_adapter = {
1659 .name = "VINO I2C bus",
1660 .algo = &sgi_algo,
1661 .algo_data = &i2c_sgi_vino_data,
1662 .owner = THIS_MODULE,
1666 * Prepare VINO for DMA transfer...
1667 * (execute only with vino_lock and input_lock locked)
1669 static int vino_dma_setup(struct vino_channel_settings *vcs,
1670 struct vino_framebuffer *fb)
1672 u32 ctrl, intr;
1673 struct sgi_vino_channel *ch;
1674 const struct vino_data_norm *norm;
1676 dprintk("vino_dma_setup():\n");
1678 vcs->field = 0;
1679 fb->frame_counter = 0;
1681 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1682 norm = &vino_data_norms[vcs->data_norm];
1684 ch->page_index = 0;
1685 ch->line_count = 0;
1687 /* VINO line size register is set 8 bytes less than actual */
1688 ch->line_size = vcs->line_size - 8;
1690 /* let VINO know where to transfer data */
1691 ch->start_desc_tbl = fb->desc_table.dma;
1692 ch->next_4_desc = fb->desc_table.dma;
1694 /* give vino time to fetch the first four descriptors, 5 usec
1695 * should be more than enough time */
1696 udelay(VINO_DESC_FETCH_DELAY);
1698 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1699 ch->start_desc_tbl, ch->next_4_desc);
1701 /* set the alpha register */
1702 ch->alpha = vcs->alpha;
1704 /* set clipping registers */
1705 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1706 VINO_CLIP_EVEN(norm->even.top +
1707 vcs->clipping.top / 2) |
1708 VINO_CLIP_X(vcs->clipping.left);
1709 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1710 vcs->clipping.bottom / 2 - 1) |
1711 VINO_CLIP_EVEN(norm->even.top +
1712 vcs->clipping.bottom / 2 - 1) |
1713 VINO_CLIP_X(vcs->clipping.right);
1715 /* set the size of actual content in the buffer (DECIMATION !) */
1716 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1717 vcs->decimation) *
1718 ((vcs->clipping.bottom - vcs->clipping.top) /
1719 vcs->decimation) *
1720 vino_data_formats[vcs->data_format].bpp;
1722 ch->frame_rate = vcs->framert_reg;
1724 ctrl = vino->control;
1725 intr = vino->intr_status;
1727 if (vcs->channel == VINO_CHANNEL_A) {
1728 /* All interrupt conditions for this channel was cleared
1729 * so clear the interrupt status register and enable
1730 * interrupts */
1731 intr &= ~VINO_INTSTAT_A;
1732 ctrl |= VINO_CTRL_A_INT;
1734 /* enable synchronization */
1735 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1737 /* enable frame assembly */
1738 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1740 /* set decimation used */
1741 if (vcs->decimation < 2)
1742 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1743 else {
1744 ctrl |= VINO_CTRL_A_DEC_ENBL;
1745 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1746 ctrl |= (vcs->decimation - 1) <<
1747 VINO_CTRL_A_DEC_SCALE_SHIFT;
1750 /* select input interface */
1751 if (vcs->input == VINO_INPUT_D1)
1752 ctrl |= VINO_CTRL_A_SELECT;
1753 else
1754 ctrl &= ~VINO_CTRL_A_SELECT;
1756 /* palette */
1757 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1758 VINO_CTRL_A_DITHER);
1759 } else {
1760 intr &= ~VINO_INTSTAT_B;
1761 ctrl |= VINO_CTRL_B_INT;
1763 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1764 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1766 if (vcs->decimation < 2)
1767 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1768 else {
1769 ctrl |= VINO_CTRL_B_DEC_ENBL;
1770 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1771 ctrl |= (vcs->decimation - 1) <<
1772 VINO_CTRL_B_DEC_SCALE_SHIFT;
1775 if (vcs->input == VINO_INPUT_D1)
1776 ctrl |= VINO_CTRL_B_SELECT;
1777 else
1778 ctrl &= ~VINO_CTRL_B_SELECT;
1780 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1781 VINO_CTRL_B_DITHER);
1784 /* set palette */
1785 fb->data_format = vcs->data_format;
1787 switch (vcs->data_format) {
1788 case VINO_DATA_FMT_GREY:
1789 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1790 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1791 break;
1792 case VINO_DATA_FMT_RGB32:
1793 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1794 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1795 break;
1796 case VINO_DATA_FMT_YUV:
1797 /* nothing needs to be done */
1798 break;
1799 case VINO_DATA_FMT_RGB332:
1800 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1801 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1802 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1803 break;
1806 vino->intr_status = intr;
1807 vino->control = ctrl;
1809 return 0;
1812 /* (execute only with vino_lock locked) */
1813 static inline void vino_dma_start(struct vino_channel_settings *vcs)
1815 u32 ctrl = vino->control;
1817 dprintk("vino_dma_start():\n");
1818 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1819 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1820 vino->control = ctrl;
1823 /* (execute only with vino_lock locked) */
1824 static inline void vino_dma_stop(struct vino_channel_settings *vcs)
1826 u32 ctrl = vino->control;
1828 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1829 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1830 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1831 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
1832 vino->control = ctrl;
1833 dprintk("vino_dma_stop():\n");
1837 * Load dummy page to descriptor registers. This prevents generating of
1838 * spurious interrupts. (execute only with vino_lock locked)
1840 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1842 struct sgi_vino_channel *ch;
1844 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1846 ch->page_index = 0;
1847 ch->line_count = 0;
1849 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1850 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1852 udelay(VINO_DESC_FETCH_DELAY);
1853 dprintk("channel %c clear interrupt condition\n",
1854 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1857 static int vino_capture(struct vino_channel_settings *vcs,
1858 struct vino_framebuffer *fb)
1860 int err = 0;
1861 unsigned long flags, flags2;
1863 spin_lock_irqsave(&fb->state_lock, flags);
1865 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1866 err = -EBUSY;
1867 fb->state = VINO_FRAMEBUFFER_IN_USE;
1869 spin_unlock_irqrestore(&fb->state_lock, flags);
1871 if (err)
1872 return err;
1874 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1875 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1877 vino_dma_setup(vcs, fb);
1878 vino_dma_start(vcs);
1880 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1881 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1883 return err;
1886 static
1887 struct vino_framebuffer *vino_capture_enqueue(struct
1888 vino_channel_settings *vcs,
1889 unsigned int index)
1891 struct vino_framebuffer *fb;
1892 unsigned long flags;
1894 dprintk("vino_capture_enqueue():\n");
1896 spin_lock_irqsave(&vcs->capture_lock, flags);
1898 fb = vino_queue_add(&vcs->fb_queue, index);
1899 if (fb == NULL) {
1900 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1901 "queue full?\n");
1902 goto out;
1904 out:
1905 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1907 return fb;
1910 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1912 struct vino_framebuffer *fb;
1913 unsigned int incoming, id;
1914 int err = 0;
1915 unsigned long flags;
1917 dprintk("vino_capture_next():\n");
1919 spin_lock_irqsave(&vcs->capture_lock, flags);
1921 if (start) {
1922 /* start capture only if capture isn't in progress already */
1923 if (vcs->capturing) {
1924 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1925 return 0;
1928 } else {
1929 /* capture next frame:
1930 * stop capture if capturing is not set */
1931 if (!vcs->capturing) {
1932 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1933 return 0;
1937 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
1938 if (err) {
1939 dprintk("vino_capture_next(): vino_queue_get_incoming() "
1940 "failed\n");
1941 err = -EINVAL;
1942 goto out;
1944 if (incoming == 0) {
1945 dprintk("vino_capture_next(): no buffers available\n");
1946 goto out;
1949 fb = vino_queue_peek(&vcs->fb_queue, &id);
1950 if (fb == NULL) {
1951 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
1952 err = -EINVAL;
1953 goto out;
1956 if (start) {
1957 vcs->capturing = 1;
1960 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1962 err = vino_capture(vcs, fb);
1964 return err;
1966 out:
1967 vcs->capturing = 0;
1968 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1970 return err;
1973 static inline int vino_is_capturing(struct vino_channel_settings *vcs)
1975 int ret;
1976 unsigned long flags;
1978 spin_lock_irqsave(&vcs->capture_lock, flags);
1980 ret = vcs->capturing;
1982 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1984 return ret;
1987 /* waits until a frame is captured */
1988 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
1990 wait_queue_t wait;
1991 int err = 0;
1993 dprintk("vino_wait_for_frame():\n");
1995 init_waitqueue_entry(&wait, current);
1996 /* add ourselves into wait queue */
1997 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
1999 /* to ensure that schedule_timeout will return immediately
2000 * if VINO interrupt was triggered meanwhile */
2001 schedule_timeout_interruptible(msecs_to_jiffies(100));
2003 if (signal_pending(current))
2004 err = -EINTR;
2006 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2008 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2009 err ? "failed" : "ok");
2011 return err;
2014 /* the function assumes that PAGE_SIZE % 4 == 0 */
2015 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2016 unsigned char *pageptr;
2017 unsigned int page, i;
2018 unsigned char a;
2020 for (page = 0; page < fb->desc_table.page_count; page++) {
2021 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2023 for (i = 0; i < PAGE_SIZE; i += 4) {
2024 a = pageptr[0];
2025 pageptr[0] = pageptr[3];
2026 pageptr[1] = pageptr[2];
2027 pageptr[2] = pageptr[1];
2028 pageptr[3] = a;
2029 pageptr += 4;
2034 /* checks if the buffer is in correct state and syncs data */
2035 static int vino_check_buffer(struct vino_channel_settings *vcs,
2036 struct vino_framebuffer *fb)
2038 int err = 0;
2039 unsigned long flags;
2041 dprintk("vino_check_buffer():\n");
2043 spin_lock_irqsave(&fb->state_lock, flags);
2044 switch (fb->state) {
2045 case VINO_FRAMEBUFFER_IN_USE:
2046 err = -EIO;
2047 break;
2048 case VINO_FRAMEBUFFER_READY:
2049 vino_sync_buffer(fb);
2050 fb->state = VINO_FRAMEBUFFER_UNUSED;
2051 break;
2052 default:
2053 err = -EINVAL;
2055 spin_unlock_irqrestore(&fb->state_lock, flags);
2057 if (!err) {
2058 if (vino_pixel_conversion
2059 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2060 vino_convert_to_rgba(fb);
2062 } else if (err && (err != -EINVAL)) {
2063 dprintk("vino_check_buffer(): buffer not ready\n");
2065 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2066 vino_dma_stop(vcs);
2067 vino_clear_interrupt(vcs);
2068 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2071 return err;
2074 /* forcefully terminates capture */
2075 static void vino_capture_stop(struct vino_channel_settings *vcs)
2077 unsigned int incoming = 0, outgoing = 0, id;
2078 unsigned long flags, flags2;
2080 dprintk("vino_capture_stop():\n");
2082 spin_lock_irqsave(&vcs->capture_lock, flags);
2084 /* unset capturing to stop queue processing */
2085 vcs->capturing = 0;
2087 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2089 vino_dma_stop(vcs);
2090 vino_clear_interrupt(vcs);
2092 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2094 /* remove all items from the queue */
2095 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2096 dprintk("vino_capture_stop(): "
2097 "vino_queue_get_incoming() failed\n");
2098 goto out;
2100 while (incoming > 0) {
2101 vino_queue_transfer(&vcs->fb_queue);
2103 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2104 dprintk("vino_capture_stop(): "
2105 "vino_queue_get_incoming() failed\n");
2106 goto out;
2110 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2111 dprintk("vino_capture_stop(): "
2112 "vino_queue_get_outgoing() failed\n");
2113 goto out;
2115 while (outgoing > 0) {
2116 vino_queue_remove(&vcs->fb_queue, &id);
2118 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2119 dprintk("vino_capture_stop(): "
2120 "vino_queue_get_outgoing() failed\n");
2121 goto out;
2125 out:
2126 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2130 static void vino_skip_frame(struct vino_channel_settings *vcs)
2132 struct vino_framebuffer *fb;
2133 unsigned long flags;
2134 unsigned int id;
2136 spin_lock_irqsave(&vcs->capture_lock, flags);
2137 fb = vino_queue_peek(&vcs->fb_queue, &id);
2138 if (!fb) {
2139 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2140 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2141 return;
2143 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2145 spin_lock_irqsave(&fb->state_lock, flags);
2146 fb->state = VINO_FRAMEBUFFER_UNUSED;
2147 spin_unlock_irqrestore(&fb->state_lock, flags);
2149 vino_capture_next(vcs, 0);
2152 static void vino_frame_done(struct vino_channel_settings *vcs)
2154 struct vino_framebuffer *fb;
2155 unsigned long flags;
2157 spin_lock_irqsave(&vcs->capture_lock, flags);
2158 fb = vino_queue_transfer(&vcs->fb_queue);
2159 if (!fb) {
2160 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2161 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2162 return;
2164 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2166 fb->frame_counter = vcs->int_data.frame_counter;
2167 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2168 sizeof(struct timeval));
2170 spin_lock_irqsave(&fb->state_lock, flags);
2171 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2172 fb->state = VINO_FRAMEBUFFER_READY;
2173 spin_unlock_irqrestore(&fb->state_lock, flags);
2175 wake_up(&vcs->fb_queue.frame_wait_queue);
2177 vino_capture_next(vcs, 0);
2180 static void vino_capture_tasklet(unsigned long channel) {
2181 struct vino_channel_settings *vcs;
2183 vcs = (channel == VINO_CHANNEL_A)
2184 ? &vino_drvdata->a : &vino_drvdata->b;
2186 if (vcs->int_data.skip)
2187 vcs->int_data.skip_count++;
2189 if (vcs->int_data.skip && (vcs->int_data.skip_count
2190 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2191 vino_skip_frame(vcs);
2192 } else {
2193 vcs->int_data.skip_count = 0;
2194 vino_frame_done(vcs);
2198 static irqreturn_t vino_interrupt(int irq, void *dev_id)
2200 u32 ctrl, intr;
2201 unsigned int fc_a, fc_b;
2202 int handled_a = 0, skip_a = 0, done_a = 0;
2203 int handled_b = 0, skip_b = 0, done_b = 0;
2205 #ifdef VINO_DEBUG_INT
2206 int loop = 0;
2207 unsigned int line_count = vino->a.line_count,
2208 page_index = vino->a.page_index,
2209 field_counter = vino->a.field_counter,
2210 start_desc_tbl = vino->a.start_desc_tbl,
2211 next_4_desc = vino->a.next_4_desc;
2212 unsigned int line_count_2,
2213 page_index_2,
2214 field_counter_2,
2215 start_desc_tbl_2,
2216 next_4_desc_2;
2217 #endif
2219 spin_lock(&vino_drvdata->vino_lock);
2221 while ((intr = vino->intr_status)) {
2222 fc_a = vino->a.field_counter >> 1;
2223 fc_b = vino->b.field_counter >> 1;
2225 /* handle error-interrupts in some special way ?
2226 * --> skips frames */
2227 if (intr & VINO_INTSTAT_A) {
2228 if (intr & VINO_INTSTAT_A_EOF) {
2229 vino_drvdata->a.field++;
2230 if (vino_drvdata->a.field > 1) {
2231 vino_dma_stop(&vino_drvdata->a);
2232 vino_clear_interrupt(&vino_drvdata->a);
2233 vino_drvdata->a.field = 0;
2234 done_a = 1;
2235 } else {
2236 if (vino->a.page_index
2237 != vino_drvdata->a.line_size) {
2238 vino->a.line_count = 0;
2239 vino->a.page_index =
2240 vino_drvdata->
2241 a.line_size;
2242 vino->a.next_4_desc =
2243 vino->a.start_desc_tbl;
2246 dprintk("channel A end-of-field "
2247 "interrupt: %04x\n", intr);
2248 } else {
2249 vino_dma_stop(&vino_drvdata->a);
2250 vino_clear_interrupt(&vino_drvdata->a);
2251 vino_drvdata->a.field = 0;
2252 skip_a = 1;
2253 dprintk("channel A error interrupt: %04x\n",
2254 intr);
2257 #ifdef VINO_DEBUG_INT
2258 line_count_2 = vino->a.line_count;
2259 page_index_2 = vino->a.page_index;
2260 field_counter_2 = vino->a.field_counter;
2261 start_desc_tbl_2 = vino->a.start_desc_tbl;
2262 next_4_desc_2 = vino->a.next_4_desc;
2264 printk("intr = %04x, loop = %d, field = %d\n",
2265 intr, loop, vino_drvdata->a.field);
2266 printk("1- line count = %04d, page index = %04d, "
2267 "start = %08x, next = %08x\n"
2268 " fieldc = %d, framec = %d\n",
2269 line_count, page_index, start_desc_tbl,
2270 next_4_desc, field_counter, fc_a);
2271 printk("12-line count = %04d, page index = %04d, "
2272 " start = %08x, next = %08x\n",
2273 line_count_2, page_index_2, start_desc_tbl_2,
2274 next_4_desc_2);
2276 if (done_a)
2277 printk("\n");
2278 #endif
2281 if (intr & VINO_INTSTAT_B) {
2282 if (intr & VINO_INTSTAT_B_EOF) {
2283 vino_drvdata->b.field++;
2284 if (vino_drvdata->b.field > 1) {
2285 vino_dma_stop(&vino_drvdata->b);
2286 vino_clear_interrupt(&vino_drvdata->b);
2287 vino_drvdata->b.field = 0;
2288 done_b = 1;
2290 dprintk("channel B end-of-field "
2291 "interrupt: %04x\n", intr);
2292 } else {
2293 vino_dma_stop(&vino_drvdata->b);
2294 vino_clear_interrupt(&vino_drvdata->b);
2295 vino_drvdata->b.field = 0;
2296 skip_b = 1;
2297 dprintk("channel B error interrupt: %04x\n",
2298 intr);
2302 /* Always remember to clear interrupt status.
2303 * Disable VINO interrupts while we do this. */
2304 ctrl = vino->control;
2305 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2306 vino->intr_status = ~intr;
2307 vino->control = ctrl;
2309 spin_unlock(&vino_drvdata->vino_lock);
2311 if ((!handled_a) && (done_a || skip_a)) {
2312 if (!skip_a) {
2313 do_gettimeofday(&vino_drvdata->
2314 a.int_data.timestamp);
2315 vino_drvdata->a.int_data.frame_counter = fc_a;
2317 vino_drvdata->a.int_data.skip = skip_a;
2319 dprintk("channel A %s, interrupt: %d\n",
2320 skip_a ? "skipping frame" : "frame done",
2321 intr);
2322 tasklet_hi_schedule(&vino_tasklet_a);
2323 handled_a = 1;
2326 if ((!handled_b) && (done_b || skip_b)) {
2327 if (!skip_b) {
2328 do_gettimeofday(&vino_drvdata->
2329 b.int_data.timestamp);
2330 vino_drvdata->b.int_data.frame_counter = fc_b;
2332 vino_drvdata->b.int_data.skip = skip_b;
2334 dprintk("channel B %s, interrupt: %d\n",
2335 skip_b ? "skipping frame" : "frame done",
2336 intr);
2337 tasklet_hi_schedule(&vino_tasklet_b);
2338 handled_b = 1;
2341 #ifdef VINO_DEBUG_INT
2342 loop++;
2343 #endif
2344 spin_lock(&vino_drvdata->vino_lock);
2347 spin_unlock(&vino_drvdata->vino_lock);
2349 return IRQ_HANDLED;
2352 /* VINO video input management */
2354 static int vino_get_saa7191_input(int input)
2356 switch (input) {
2357 case VINO_INPUT_COMPOSITE:
2358 return SAA7191_INPUT_COMPOSITE;
2359 case VINO_INPUT_SVIDEO:
2360 return SAA7191_INPUT_SVIDEO;
2361 default:
2362 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2363 "invalid input!\n");
2364 return -1;
2368 /* execute with input_lock locked */
2369 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2371 switch(vcs->input) {
2372 case VINO_INPUT_COMPOSITE:
2373 case VINO_INPUT_SVIDEO:
2374 return vino_drvdata->decoder_owner == vcs->channel;
2375 case VINO_INPUT_D1:
2376 return vino_drvdata->camera_owner == vcs->channel;
2377 default:
2378 return 0;
2382 static int vino_acquire_input(struct vino_channel_settings *vcs)
2384 unsigned long flags;
2385 int ret = 0;
2387 dprintk("vino_acquire_input():\n");
2389 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2391 /* First try D1 and then SAA7191 */
2392 if (vino_drvdata->camera
2393 && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2394 vino_drvdata->camera_owner = vcs->channel;
2395 vcs->input = VINO_INPUT_D1;
2396 vcs->data_norm = VINO_DATA_NORM_D1;
2397 } else if (vino_drvdata->decoder
2398 && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
2399 int input;
2400 int data_norm;
2401 v4l2_std_id norm;
2403 input = VINO_INPUT_COMPOSITE;
2405 ret = decoder_call(video, s_routing,
2406 vino_get_saa7191_input(input), 0, 0);
2407 if (ret) {
2408 ret = -EINVAL;
2409 goto out;
2412 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2414 /* Don't hold spinlocks while auto-detecting norm
2415 * as it may take a while... */
2417 ret = decoder_call(video, querystd, &norm);
2418 if (!ret) {
2419 for (data_norm = 0; data_norm < 3; data_norm++) {
2420 if (vino_data_norms[data_norm].std & norm)
2421 break;
2423 if (data_norm == 3)
2424 data_norm = VINO_DATA_NORM_PAL;
2425 ret = decoder_call(core, s_std, norm);
2428 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2430 if (ret) {
2431 ret = -EINVAL;
2432 goto out;
2435 vino_drvdata->decoder_owner = vcs->channel;
2437 vcs->input = input;
2438 vcs->data_norm = data_norm;
2439 } else {
2440 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2441 vino_drvdata->b.input : vino_drvdata->a.input;
2442 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2443 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2446 if (vcs->input == VINO_INPUT_NONE) {
2447 ret = -ENODEV;
2448 goto out;
2451 vino_set_default_clipping(vcs);
2452 vino_set_default_scaling(vcs);
2453 vino_set_default_framerate(vcs);
2455 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2457 out:
2458 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2460 return ret;
2463 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2465 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2466 &vino_drvdata->b : &vino_drvdata->a;
2467 unsigned long flags;
2468 int ret = 0;
2470 dprintk("vino_set_input():\n");
2472 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2474 if (vcs->input == input)
2475 goto out;
2477 switch (input) {
2478 case VINO_INPUT_COMPOSITE:
2479 case VINO_INPUT_SVIDEO:
2480 if (!vino_drvdata->decoder) {
2481 ret = -EINVAL;
2482 goto out;
2485 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2486 vino_drvdata->decoder_owner = vcs->channel;
2489 if (vino_drvdata->decoder_owner == vcs->channel) {
2490 int data_norm;
2491 v4l2_std_id norm;
2493 ret = decoder_call(video, s_routing,
2494 vino_get_saa7191_input(input), 0, 0);
2495 if (ret) {
2496 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2497 ret = -EINVAL;
2498 goto out;
2501 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2503 /* Don't hold spinlocks while auto-detecting norm
2504 * as it may take a while... */
2506 ret = decoder_call(video, querystd, &norm);
2507 if (!ret) {
2508 for (data_norm = 0; data_norm < 3; data_norm++) {
2509 if (vino_data_norms[data_norm].std & norm)
2510 break;
2512 if (data_norm == 3)
2513 data_norm = VINO_DATA_NORM_PAL;
2514 ret = decoder_call(core, s_std, norm);
2517 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2519 if (ret) {
2520 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2521 ret = -EINVAL;
2522 goto out;
2525 vcs->input = input;
2526 vcs->data_norm = data_norm;
2527 } else {
2528 if (input != vcs2->input) {
2529 ret = -EBUSY;
2530 goto out;
2533 vcs->input = input;
2534 vcs->data_norm = vcs2->data_norm;
2537 if (vino_drvdata->camera_owner == vcs->channel) {
2538 /* Transfer the ownership or release the input */
2539 if (vcs2->input == VINO_INPUT_D1) {
2540 vino_drvdata->camera_owner = vcs2->channel;
2541 } else {
2542 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2545 break;
2546 case VINO_INPUT_D1:
2547 if (!vino_drvdata->camera) {
2548 ret = -EINVAL;
2549 goto out;
2552 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2553 vino_drvdata->camera_owner = vcs->channel;
2555 if (vino_drvdata->decoder_owner == vcs->channel) {
2556 /* Transfer the ownership or release the input */
2557 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2558 (vcs2->input == VINO_INPUT_SVIDEO)) {
2559 vino_drvdata->decoder_owner = vcs2->channel;
2560 } else {
2561 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2565 vcs->input = input;
2566 vcs->data_norm = VINO_DATA_NORM_D1;
2567 break;
2568 default:
2569 ret = -EINVAL;
2570 goto out;
2573 vino_set_default_clipping(vcs);
2574 vino_set_default_scaling(vcs);
2575 vino_set_default_framerate(vcs);
2577 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2579 out:
2580 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2582 return ret;
2585 static void vino_release_input(struct vino_channel_settings *vcs)
2587 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2588 &vino_drvdata->b : &vino_drvdata->a;
2589 unsigned long flags;
2591 dprintk("vino_release_input():\n");
2593 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2595 /* Release ownership of the channel
2596 * and if the other channel takes input from
2597 * the same source, transfer the ownership */
2598 if (vino_drvdata->camera_owner == vcs->channel) {
2599 if (vcs2->input == VINO_INPUT_D1) {
2600 vino_drvdata->camera_owner = vcs2->channel;
2601 } else {
2602 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2604 } else if (vino_drvdata->decoder_owner == vcs->channel) {
2605 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2606 (vcs2->input == VINO_INPUT_SVIDEO)) {
2607 vino_drvdata->decoder_owner = vcs2->channel;
2608 } else {
2609 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2612 vcs->input = VINO_INPUT_NONE;
2614 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2617 /* execute with input_lock locked */
2618 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2619 unsigned int data_norm,
2620 unsigned long *flags)
2622 int err = 0;
2624 if (data_norm == vcs->data_norm)
2625 return 0;
2627 switch (vcs->input) {
2628 case VINO_INPUT_D1:
2629 /* only one "norm" supported */
2630 if (data_norm != VINO_DATA_NORM_D1)
2631 return -EINVAL;
2632 break;
2633 case VINO_INPUT_COMPOSITE:
2634 case VINO_INPUT_SVIDEO: {
2635 v4l2_std_id norm;
2637 if ((data_norm != VINO_DATA_NORM_PAL)
2638 && (data_norm != VINO_DATA_NORM_NTSC)
2639 && (data_norm != VINO_DATA_NORM_SECAM))
2640 return -EINVAL;
2642 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
2644 /* Don't hold spinlocks while setting norm
2645 * as it may take a while... */
2647 norm = vino_data_norms[data_norm].std;
2648 err = decoder_call(core, s_std, norm);
2650 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2652 if (err)
2653 goto out;
2655 vcs->data_norm = data_norm;
2657 vino_set_default_clipping(vcs);
2658 vino_set_default_scaling(vcs);
2659 vino_set_default_framerate(vcs);
2660 break;
2662 default:
2663 return -EINVAL;
2666 out:
2667 return err;
2670 /* V4L2 helper functions */
2672 static int vino_find_data_format(__u32 pixelformat)
2674 int i;
2676 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2677 if (vino_data_formats[i].pixelformat == pixelformat)
2678 return i;
2681 return VINO_DATA_FMT_NONE;
2684 static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
2686 int input = VINO_INPUT_NONE;
2687 unsigned long flags;
2689 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2690 if (vino_drvdata->decoder && vino_drvdata->camera) {
2691 switch (index) {
2692 case 0:
2693 input = VINO_INPUT_COMPOSITE;
2694 break;
2695 case 1:
2696 input = VINO_INPUT_SVIDEO;
2697 break;
2698 case 2:
2699 input = VINO_INPUT_D1;
2700 break;
2702 } else if (vino_drvdata->decoder) {
2703 switch (index) {
2704 case 0:
2705 input = VINO_INPUT_COMPOSITE;
2706 break;
2707 case 1:
2708 input = VINO_INPUT_SVIDEO;
2709 break;
2711 } else if (vino_drvdata->camera) {
2712 switch (index) {
2713 case 0:
2714 input = VINO_INPUT_D1;
2715 break;
2718 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2720 return input;
2723 /* execute with input_lock locked */
2724 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2726 __u32 index = 0;
2728 if (vino_drvdata->decoder && vino_drvdata->camera) {
2729 switch (vcs->input) {
2730 case VINO_INPUT_COMPOSITE:
2731 index = 0;
2732 break;
2733 case VINO_INPUT_SVIDEO:
2734 index = 1;
2735 break;
2736 case VINO_INPUT_D1:
2737 index = 2;
2738 break;
2740 } else if (vino_drvdata->decoder) {
2741 switch (vcs->input) {
2742 case VINO_INPUT_COMPOSITE:
2743 index = 0;
2744 break;
2745 case VINO_INPUT_SVIDEO:
2746 index = 1;
2747 break;
2749 } else if (vino_drvdata->camera) {
2750 switch (vcs->input) {
2751 case VINO_INPUT_D1:
2752 index = 0;
2753 break;
2757 return index;
2760 /* V4L2 ioctls */
2762 static int vino_querycap(struct file *file, void *__fh,
2763 struct v4l2_capability *cap)
2765 memset(cap, 0, sizeof(struct v4l2_capability));
2767 strcpy(cap->driver, vino_driver_name);
2768 strcpy(cap->card, vino_driver_description);
2769 strcpy(cap->bus_info, vino_bus_name);
2770 cap->version = VINO_VERSION_CODE;
2771 cap->capabilities =
2772 V4L2_CAP_VIDEO_CAPTURE |
2773 V4L2_CAP_STREAMING;
2774 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2775 return 0;
2778 static int vino_enum_input(struct file *file, void *__fh,
2779 struct v4l2_input *i)
2781 struct vino_channel_settings *vcs = video_drvdata(file);
2782 __u32 index = i->index;
2783 int input;
2784 dprintk("requested index = %d\n", index);
2786 input = vino_int_enum_input(vcs, index);
2787 if (input == VINO_INPUT_NONE)
2788 return -EINVAL;
2790 memset(i, 0, sizeof(struct v4l2_input));
2792 i->index = index;
2793 i->type = V4L2_INPUT_TYPE_CAMERA;
2794 i->std = vino_inputs[input].std;
2795 strcpy(i->name, vino_inputs[input].name);
2797 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2798 decoder_call(video, g_input_status, &i->status);
2799 return 0;
2802 static int vino_g_input(struct file *file, void *__fh,
2803 unsigned int *i)
2805 struct vino_channel_settings *vcs = video_drvdata(file);
2806 __u32 index;
2807 int input;
2808 unsigned long flags;
2810 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2811 input = vcs->input;
2812 index = vino_find_input_index(vcs);
2813 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2815 dprintk("input = %d\n", input);
2817 if (input == VINO_INPUT_NONE) {
2818 return -EINVAL;
2821 *i = index;
2823 return 0;
2826 static int vino_s_input(struct file *file, void *__fh,
2827 unsigned int i)
2829 struct vino_channel_settings *vcs = video_drvdata(file);
2830 int input;
2831 dprintk("requested input = %d\n", i);
2833 input = vino_int_enum_input(vcs, i);
2834 if (input == VINO_INPUT_NONE)
2835 return -EINVAL;
2837 return vino_set_input(vcs, input);
2840 static int vino_querystd(struct file *file, void *__fh,
2841 v4l2_std_id *std)
2843 struct vino_channel_settings *vcs = video_drvdata(file);
2844 unsigned long flags;
2845 int err = 0;
2847 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2849 switch (vcs->input) {
2850 case VINO_INPUT_D1:
2851 *std = vino_inputs[vcs->input].std;
2852 break;
2853 case VINO_INPUT_COMPOSITE:
2854 case VINO_INPUT_SVIDEO: {
2855 decoder_call(video, querystd, std);
2856 break;
2858 default:
2859 err = -EINVAL;
2862 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2864 return err;
2867 static int vino_g_std(struct file *file, void *__fh,
2868 v4l2_std_id *std)
2870 struct vino_channel_settings *vcs = video_drvdata(file);
2871 unsigned long flags;
2873 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2875 *std = vino_data_norms[vcs->data_norm].std;
2876 dprintk("current standard = %d\n", vcs->data_norm);
2878 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2880 return 0;
2883 static int vino_s_std(struct file *file, void *__fh,
2884 v4l2_std_id *std)
2886 struct vino_channel_settings *vcs = video_drvdata(file);
2887 unsigned long flags;
2888 int ret = 0;
2890 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2892 if (!vino_is_input_owner(vcs)) {
2893 ret = -EBUSY;
2894 goto out;
2897 /* check if the standard is valid for the current input */
2898 if ((*std) & vino_inputs[vcs->input].std) {
2899 dprintk("standard accepted\n");
2901 /* change the video norm for SAA7191
2902 * and accept NTSC for D1 (do nothing) */
2904 if (vcs->input == VINO_INPUT_D1)
2905 goto out;
2907 if ((*std) & V4L2_STD_PAL) {
2908 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
2909 &flags);
2910 } else if ((*std) & V4L2_STD_NTSC) {
2911 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
2912 &flags);
2913 } else if ((*std) & V4L2_STD_SECAM) {
2914 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
2915 &flags);
2916 } else {
2917 ret = -EINVAL;
2920 if (ret) {
2921 ret = -EINVAL;
2923 } else {
2924 ret = -EINVAL;
2927 out:
2928 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2930 return ret;
2933 static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
2934 struct v4l2_fmtdesc *fd)
2936 dprintk("format index = %d\n", fd->index);
2938 if (fd->index >= VINO_DATA_FMT_COUNT)
2939 return -EINVAL;
2940 dprintk("format name = %s\n", vino_data_formats[fd->index].description);
2942 fd->pixelformat = vino_data_formats[fd->index].pixelformat;
2943 strcpy(fd->description, vino_data_formats[fd->index].description);
2944 return 0;
2947 static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
2948 struct v4l2_format *f)
2950 struct vino_channel_settings *vcs = video_drvdata(file);
2951 struct vino_channel_settings tempvcs;
2952 unsigned long flags;
2953 struct v4l2_pix_format *pf = &f->fmt.pix;
2955 dprintk("requested: w = %d, h = %d\n",
2956 pf->width, pf->height);
2958 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2959 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
2960 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2962 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
2963 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
2964 tempvcs.data_format = VINO_DATA_FMT_GREY;
2965 pf->pixelformat =
2966 vino_data_formats[tempvcs.data_format].
2967 pixelformat;
2970 /* data format must be set before clipping/scaling */
2971 vino_set_scaling(&tempvcs, pf->width, pf->height);
2973 dprintk("data format = %s\n",
2974 vino_data_formats[tempvcs.data_format].description);
2976 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
2977 tempvcs.decimation;
2978 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
2979 tempvcs.decimation;
2981 pf->field = V4L2_FIELD_INTERLACED;
2982 pf->bytesperline = tempvcs.line_size;
2983 pf->sizeimage = tempvcs.line_size *
2984 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
2985 tempvcs.decimation;
2986 pf->colorspace =
2987 vino_data_formats[tempvcs.data_format].colorspace;
2989 pf->priv = 0;
2990 return 0;
2993 static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
2994 struct v4l2_format *f)
2996 struct vino_channel_settings *vcs = video_drvdata(file);
2997 unsigned long flags;
2998 struct v4l2_pix_format *pf = &f->fmt.pix;
3000 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3002 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3003 vcs->decimation;
3004 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3005 vcs->decimation;
3006 pf->pixelformat =
3007 vino_data_formats[vcs->data_format].pixelformat;
3009 pf->field = V4L2_FIELD_INTERLACED;
3010 pf->bytesperline = vcs->line_size;
3011 pf->sizeimage = vcs->line_size *
3012 (vcs->clipping.bottom - vcs->clipping.top) /
3013 vcs->decimation;
3014 pf->colorspace =
3015 vino_data_formats[vcs->data_format].colorspace;
3017 pf->priv = 0;
3019 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3020 return 0;
3023 static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3024 struct v4l2_format *f)
3026 struct vino_channel_settings *vcs = video_drvdata(file);
3027 int data_format;
3028 unsigned long flags;
3029 struct v4l2_pix_format *pf = &f->fmt.pix;
3031 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3033 data_format = vino_find_data_format(pf->pixelformat);
3035 if (data_format == VINO_DATA_FMT_NONE) {
3036 vcs->data_format = VINO_DATA_FMT_GREY;
3037 pf->pixelformat =
3038 vino_data_formats[vcs->data_format].
3039 pixelformat;
3040 } else {
3041 vcs->data_format = data_format;
3044 /* data format must be set before clipping/scaling */
3045 vino_set_scaling(vcs, pf->width, pf->height);
3047 dprintk("data format = %s\n",
3048 vino_data_formats[vcs->data_format].description);
3050 pf->width = vcs->clipping.right - vcs->clipping.left;
3051 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3053 pf->field = V4L2_FIELD_INTERLACED;
3054 pf->bytesperline = vcs->line_size;
3055 pf->sizeimage = vcs->line_size *
3056 (vcs->clipping.bottom - vcs->clipping.top) /
3057 vcs->decimation;
3058 pf->colorspace =
3059 vino_data_formats[vcs->data_format].colorspace;
3061 pf->priv = 0;
3063 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3064 return 0;
3067 static int vino_cropcap(struct file *file, void *__fh,
3068 struct v4l2_cropcap *ccap)
3070 struct vino_channel_settings *vcs = video_drvdata(file);
3071 const struct vino_data_norm *norm;
3072 unsigned long flags;
3074 switch (ccap->type) {
3075 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3076 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3078 norm = &vino_data_norms[vcs->data_norm];
3080 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3082 ccap->bounds.left = 0;
3083 ccap->bounds.top = 0;
3084 ccap->bounds.width = norm->width;
3085 ccap->bounds.height = norm->height;
3086 memcpy(&ccap->defrect, &ccap->bounds,
3087 sizeof(struct v4l2_rect));
3089 ccap->pixelaspect.numerator = 1;
3090 ccap->pixelaspect.denominator = 1;
3091 break;
3092 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3093 default:
3094 return -EINVAL;
3097 return 0;
3100 static int vino_g_crop(struct file *file, void *__fh,
3101 struct v4l2_crop *c)
3103 struct vino_channel_settings *vcs = video_drvdata(file);
3104 unsigned long flags;
3106 switch (c->type) {
3107 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3108 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3110 c->c.left = vcs->clipping.left;
3111 c->c.top = vcs->clipping.top;
3112 c->c.width = vcs->clipping.right - vcs->clipping.left;
3113 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3115 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3116 break;
3117 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3118 default:
3119 return -EINVAL;
3122 return 0;
3125 static int vino_s_crop(struct file *file, void *__fh,
3126 struct v4l2_crop *c)
3128 struct vino_channel_settings *vcs = video_drvdata(file);
3129 unsigned long flags;
3131 switch (c->type) {
3132 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3133 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3135 vino_set_clipping(vcs, c->c.left, c->c.top,
3136 c->c.width, c->c.height);
3138 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3139 break;
3140 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3141 default:
3142 return -EINVAL;
3145 return 0;
3148 static int vino_g_parm(struct file *file, void *__fh,
3149 struct v4l2_streamparm *sp)
3151 struct vino_channel_settings *vcs = video_drvdata(file);
3152 unsigned long flags;
3153 struct v4l2_captureparm *cp = &sp->parm.capture;
3155 cp->capability = V4L2_CAP_TIMEPERFRAME;
3156 cp->timeperframe.numerator = 1;
3158 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3160 cp->timeperframe.denominator = vcs->fps;
3162 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3165 return 0;
3168 static int vino_s_parm(struct file *file, void *__fh,
3169 struct v4l2_streamparm *sp)
3171 struct vino_channel_settings *vcs = video_drvdata(file);
3172 unsigned long flags;
3173 struct v4l2_captureparm *cp = &sp->parm.capture;
3175 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3177 if ((cp->timeperframe.numerator == 0) ||
3178 (cp->timeperframe.denominator == 0)) {
3179 /* reset framerate */
3180 vino_set_default_framerate(vcs);
3181 } else {
3182 vino_set_framerate(vcs, cp->timeperframe.denominator /
3183 cp->timeperframe.numerator);
3186 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3188 return 0;
3191 static int vino_reqbufs(struct file *file, void *__fh,
3192 struct v4l2_requestbuffers *rb)
3194 struct vino_channel_settings *vcs = video_drvdata(file);
3196 if (vcs->reading)
3197 return -EBUSY;
3199 /* TODO: check queue type */
3200 if (rb->memory != V4L2_MEMORY_MMAP) {
3201 dprintk("type not mmap\n");
3202 return -EINVAL;
3205 dprintk("count = %d\n", rb->count);
3206 if (rb->count > 0) {
3207 if (vino_is_capturing(vcs)) {
3208 dprintk("busy, capturing\n");
3209 return -EBUSY;
3212 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3213 dprintk("busy, buffers still mapped\n");
3214 return -EBUSY;
3215 } else {
3216 vcs->streaming = 0;
3217 vino_queue_free(&vcs->fb_queue);
3218 vino_queue_init(&vcs->fb_queue, &rb->count);
3220 } else {
3221 vcs->streaming = 0;
3222 vino_capture_stop(vcs);
3223 vino_queue_free(&vcs->fb_queue);
3226 return 0;
3229 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3230 struct vino_framebuffer *fb,
3231 struct v4l2_buffer *b)
3233 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3234 fb->id)) {
3235 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3236 b->flags |= V4L2_BUF_FLAG_DONE;
3237 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3238 fb->id)) {
3239 b->flags &= ~V4L2_BUF_FLAG_DONE;
3240 b->flags |= V4L2_BUF_FLAG_QUEUED;
3241 } else {
3242 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3243 V4L2_BUF_FLAG_QUEUED);
3246 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3248 if (fb->map_count > 0)
3249 b->flags |= V4L2_BUF_FLAG_MAPPED;
3251 b->index = fb->id;
3252 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3253 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3254 b->m.offset = fb->offset;
3255 b->bytesused = fb->data_size;
3256 b->length = fb->size;
3257 b->field = V4L2_FIELD_INTERLACED;
3258 b->sequence = fb->frame_counter;
3259 memcpy(&b->timestamp, &fb->timestamp,
3260 sizeof(struct timeval));
3261 // b->input ?
3263 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3264 fb->id, fb->size, fb->data_size, fb->offset);
3267 static int vino_querybuf(struct file *file, void *__fh,
3268 struct v4l2_buffer *b)
3270 struct vino_channel_settings *vcs = video_drvdata(file);
3271 struct vino_framebuffer *fb;
3273 if (vcs->reading)
3274 return -EBUSY;
3276 /* TODO: check queue type */
3277 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3278 dprintk("invalid index = %d\n",
3279 b->index);
3280 return -EINVAL;
3283 fb = vino_queue_get_buffer(&vcs->fb_queue,
3284 b->index);
3285 if (fb == NULL) {
3286 dprintk("vino_queue_get_buffer() failed");
3287 return -EINVAL;
3290 vino_v4l2_get_buffer_status(vcs, fb, b);
3292 return 0;
3295 static int vino_qbuf(struct file *file, void *__fh,
3296 struct v4l2_buffer *b)
3298 struct vino_channel_settings *vcs = video_drvdata(file);
3299 struct vino_framebuffer *fb;
3300 int ret;
3302 if (vcs->reading)
3303 return -EBUSY;
3305 /* TODO: check queue type */
3306 if (b->memory != V4L2_MEMORY_MMAP) {
3307 dprintk("type not mmap\n");
3308 return -EINVAL;
3311 fb = vino_capture_enqueue(vcs, b->index);
3312 if (fb == NULL)
3313 return -EINVAL;
3315 vino_v4l2_get_buffer_status(vcs, fb, b);
3317 if (vcs->streaming) {
3318 ret = vino_capture_next(vcs, 1);
3319 if (ret)
3320 return ret;
3323 return 0;
3326 static int vino_dqbuf(struct file *file, void *__fh,
3327 struct v4l2_buffer *b)
3329 struct vino_channel_settings *vcs = video_drvdata(file);
3330 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3331 struct vino_framebuffer *fb;
3332 unsigned int incoming, outgoing;
3333 int err;
3335 if (vcs->reading)
3336 return -EBUSY;
3338 /* TODO: check queue type */
3340 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3341 if (err) {
3342 dprintk("vino_queue_get_incoming() failed\n");
3343 return -EINVAL;
3345 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3346 if (err) {
3347 dprintk("vino_queue_get_outgoing() failed\n");
3348 return -EINVAL;
3351 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3353 if (outgoing == 0) {
3354 if (incoming == 0) {
3355 dprintk("no incoming or outgoing buffers\n");
3356 return -EINVAL;
3358 if (nonblocking) {
3359 dprintk("non-blocking I/O was selected and "
3360 "there are no buffers to dequeue\n");
3361 return -EAGAIN;
3364 err = vino_wait_for_frame(vcs);
3365 if (err) {
3366 err = vino_wait_for_frame(vcs);
3367 if (err) {
3368 /* interrupted or no frames captured because of
3369 * frame skipping */
3370 /* vino_capture_failed(vcs); */
3371 return -EIO;
3376 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3377 if (fb == NULL) {
3378 dprintk("vino_queue_remove() failed\n");
3379 return -EINVAL;
3382 err = vino_check_buffer(vcs, fb);
3384 vino_v4l2_get_buffer_status(vcs, fb, b);
3386 if (err)
3387 return -EIO;
3389 return 0;
3392 static int vino_streamon(struct file *file, void *__fh,
3393 enum v4l2_buf_type i)
3395 struct vino_channel_settings *vcs = video_drvdata(file);
3396 unsigned int incoming;
3397 int ret;
3398 if (vcs->reading)
3399 return -EBUSY;
3401 if (vcs->streaming)
3402 return 0;
3404 // TODO: check queue type
3406 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3407 dprintk("no buffers allocated\n");
3408 return -EINVAL;
3411 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3412 if (ret) {
3413 dprintk("vino_queue_get_incoming() failed\n");
3414 return -EINVAL;
3417 vcs->streaming = 1;
3419 if (incoming > 0) {
3420 ret = vino_capture_next(vcs, 1);
3421 if (ret) {
3422 vcs->streaming = 0;
3424 dprintk("couldn't start capture\n");
3425 return -EINVAL;
3429 return 0;
3432 static int vino_streamoff(struct file *file, void *__fh,
3433 enum v4l2_buf_type i)
3435 struct vino_channel_settings *vcs = video_drvdata(file);
3436 if (vcs->reading)
3437 return -EBUSY;
3439 if (!vcs->streaming)
3440 return 0;
3442 vcs->streaming = 0;
3443 vino_capture_stop(vcs);
3445 return 0;
3448 static int vino_queryctrl(struct file *file, void *__fh,
3449 struct v4l2_queryctrl *queryctrl)
3451 struct vino_channel_settings *vcs = video_drvdata(file);
3452 unsigned long flags;
3453 int i;
3454 int err = 0;
3456 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3458 switch (vcs->input) {
3459 case VINO_INPUT_D1:
3460 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3461 if (vino_indycam_v4l2_controls[i].id ==
3462 queryctrl->id) {
3463 memcpy(queryctrl,
3464 &vino_indycam_v4l2_controls[i],
3465 sizeof(struct v4l2_queryctrl));
3466 queryctrl->reserved[0] = 0;
3467 goto found;
3471 err = -EINVAL;
3472 break;
3473 case VINO_INPUT_COMPOSITE:
3474 case VINO_INPUT_SVIDEO:
3475 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3476 if (vino_saa7191_v4l2_controls[i].id ==
3477 queryctrl->id) {
3478 memcpy(queryctrl,
3479 &vino_saa7191_v4l2_controls[i],
3480 sizeof(struct v4l2_queryctrl));
3481 queryctrl->reserved[0] = 0;
3482 goto found;
3486 err = -EINVAL;
3487 break;
3488 default:
3489 err = -EINVAL;
3492 found:
3493 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3495 return err;
3498 static int vino_g_ctrl(struct file *file, void *__fh,
3499 struct v4l2_control *control)
3501 struct vino_channel_settings *vcs = video_drvdata(file);
3502 unsigned long flags;
3503 int i;
3504 int err = 0;
3506 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3508 switch (vcs->input) {
3509 case VINO_INPUT_D1: {
3510 err = -EINVAL;
3511 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3512 if (vino_indycam_v4l2_controls[i].id == control->id) {
3513 err = 0;
3514 break;
3518 if (err)
3519 goto out;
3521 err = camera_call(core, g_ctrl, control);
3522 if (err)
3523 err = -EINVAL;
3524 break;
3526 case VINO_INPUT_COMPOSITE:
3527 case VINO_INPUT_SVIDEO: {
3528 err = -EINVAL;
3529 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3530 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3531 err = 0;
3532 break;
3536 if (err)
3537 goto out;
3539 err = decoder_call(core, g_ctrl, control);
3540 if (err)
3541 err = -EINVAL;
3542 break;
3544 default:
3545 err = -EINVAL;
3548 out:
3549 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3551 return err;
3554 static int vino_s_ctrl(struct file *file, void *__fh,
3555 struct v4l2_control *control)
3557 struct vino_channel_settings *vcs = video_drvdata(file);
3558 unsigned long flags;
3559 int i;
3560 int err = 0;
3562 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3564 if (!vino_is_input_owner(vcs)) {
3565 err = -EBUSY;
3566 goto out;
3569 switch (vcs->input) {
3570 case VINO_INPUT_D1: {
3571 err = -EINVAL;
3572 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3573 if (vino_indycam_v4l2_controls[i].id == control->id) {
3574 err = 0;
3575 break;
3578 if (err)
3579 goto out;
3580 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3581 control->value > vino_indycam_v4l2_controls[i].maximum) {
3582 err = -ERANGE;
3583 goto out;
3585 err = camera_call(core, s_ctrl, control);
3586 if (err)
3587 err = -EINVAL;
3588 break;
3590 case VINO_INPUT_COMPOSITE:
3591 case VINO_INPUT_SVIDEO: {
3592 err = -EINVAL;
3593 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3594 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3595 err = 0;
3596 break;
3599 if (err)
3600 goto out;
3601 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3602 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3603 err = -ERANGE;
3604 goto out;
3607 err = decoder_call(core, s_ctrl, control);
3608 if (err)
3609 err = -EINVAL;
3610 break;
3612 default:
3613 err = -EINVAL;
3616 out:
3617 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3619 return err;
3622 /* File operations */
3624 static int vino_open(struct file *file)
3626 struct vino_channel_settings *vcs = video_drvdata(file);
3627 int ret = 0;
3628 dprintk("open(): channel = %c\n",
3629 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3631 mutex_lock(&vcs->mutex);
3633 if (vcs->users) {
3634 dprintk("open(): driver busy\n");
3635 ret = -EBUSY;
3636 goto out;
3639 ret = vino_acquire_input(vcs);
3640 if (ret) {
3641 dprintk("open(): vino_acquire_input() failed\n");
3642 goto out;
3645 vcs->users++;
3647 out:
3648 mutex_unlock(&vcs->mutex);
3650 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3652 return ret;
3655 static int vino_close(struct file *file)
3657 struct vino_channel_settings *vcs = video_drvdata(file);
3658 dprintk("close():\n");
3660 mutex_lock(&vcs->mutex);
3662 vcs->users--;
3664 if (!vcs->users) {
3665 vino_release_input(vcs);
3667 /* stop DMA and free buffers */
3668 vino_capture_stop(vcs);
3669 vino_queue_free(&vcs->fb_queue);
3672 mutex_unlock(&vcs->mutex);
3674 return 0;
3677 static void vino_vm_open(struct vm_area_struct *vma)
3679 struct vino_framebuffer *fb = vma->vm_private_data;
3681 fb->map_count++;
3682 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3685 static void vino_vm_close(struct vm_area_struct *vma)
3687 struct vino_framebuffer *fb = vma->vm_private_data;
3689 fb->map_count--;
3690 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3693 static const struct vm_operations_struct vino_vm_ops = {
3694 .open = vino_vm_open,
3695 .close = vino_vm_close,
3698 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3700 struct vino_channel_settings *vcs = video_drvdata(file);
3702 unsigned long start = vma->vm_start;
3703 unsigned long size = vma->vm_end - vma->vm_start;
3704 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3706 struct vino_framebuffer *fb = NULL;
3707 unsigned int i, length;
3708 int ret = 0;
3710 dprintk("mmap():\n");
3712 // TODO: reject mmap if already mapped
3714 if (mutex_lock_interruptible(&vcs->mutex))
3715 return -EINTR;
3717 if (vcs->reading) {
3718 ret = -EBUSY;
3719 goto out;
3722 // TODO: check queue type
3724 if (!(vma->vm_flags & VM_WRITE)) {
3725 dprintk("mmap(): app bug: PROT_WRITE please\n");
3726 ret = -EINVAL;
3727 goto out;
3729 if (!(vma->vm_flags & VM_SHARED)) {
3730 dprintk("mmap(): app bug: MAP_SHARED please\n");
3731 ret = -EINVAL;
3732 goto out;
3735 /* find the correct buffer using offset */
3736 length = vino_queue_get_length(&vcs->fb_queue);
3737 if (length == 0) {
3738 dprintk("mmap(): queue not initialized\n");
3739 ret = -EINVAL;
3740 goto out;
3743 for (i = 0; i < length; i++) {
3744 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3745 if (fb == NULL) {
3746 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3747 ret = -EINVAL;
3748 goto out;
3751 if (fb->offset == offset)
3752 goto found;
3755 dprintk("mmap(): invalid offset = %lu\n", offset);
3756 ret = -EINVAL;
3757 goto out;
3759 found:
3760 dprintk("mmap(): buffer = %d\n", i);
3762 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3763 dprintk("mmap(): failed: size = %lu > %lu\n",
3764 size, fb->desc_table.page_count * PAGE_SIZE);
3765 ret = -EINVAL;
3766 goto out;
3769 for (i = 0; i < fb->desc_table.page_count; i++) {
3770 unsigned long pfn =
3771 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3772 PAGE_SHIFT;
3774 if (size < PAGE_SIZE)
3775 break;
3777 // protection was: PAGE_READONLY
3778 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3779 vma->vm_page_prot)) {
3780 dprintk("mmap(): remap_pfn_range() failed\n");
3781 ret = -EAGAIN;
3782 goto out;
3785 start += PAGE_SIZE;
3786 size -= PAGE_SIZE;
3789 fb->map_count = 1;
3791 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3792 vma->vm_flags &= ~VM_IO;
3793 vma->vm_private_data = fb;
3794 vma->vm_file = file;
3795 vma->vm_ops = &vino_vm_ops;
3797 out:
3798 mutex_unlock(&vcs->mutex);
3800 return ret;
3803 static unsigned int vino_poll(struct file *file, poll_table *pt)
3805 struct vino_channel_settings *vcs = video_drvdata(file);
3806 unsigned int outgoing;
3807 unsigned int ret = 0;
3809 // lock mutex (?)
3810 // TODO: this has to be corrected for different read modes
3812 dprintk("poll():\n");
3814 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3815 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3816 ret = POLLERR;
3817 goto error;
3819 if (outgoing > 0)
3820 goto over;
3822 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3824 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3825 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3826 ret = POLLERR;
3827 goto error;
3830 over:
3831 dprintk("poll(): data %savailable\n",
3832 (outgoing > 0) ? "" : "not ");
3834 if (outgoing > 0)
3835 ret = POLLIN | POLLRDNORM;
3837 error:
3838 return ret;
3841 static long vino_ioctl(struct file *file,
3842 unsigned int cmd, unsigned long arg)
3844 struct vino_channel_settings *vcs = video_drvdata(file);
3845 long ret;
3847 if (mutex_lock_interruptible(&vcs->mutex))
3848 return -EINTR;
3850 ret = video_ioctl2(file, cmd, arg);
3852 mutex_unlock(&vcs->mutex);
3854 return ret;
3857 /* Initialization and cleanup */
3859 /* __initdata */
3860 static int vino_init_stage;
3862 const struct v4l2_ioctl_ops vino_ioctl_ops = {
3863 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
3864 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
3865 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
3866 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
3867 .vidioc_querycap = vino_querycap,
3868 .vidioc_enum_input = vino_enum_input,
3869 .vidioc_g_input = vino_g_input,
3870 .vidioc_s_input = vino_s_input,
3871 .vidioc_g_std = vino_g_std,
3872 .vidioc_s_std = vino_s_std,
3873 .vidioc_querystd = vino_querystd,
3874 .vidioc_cropcap = vino_cropcap,
3875 .vidioc_s_crop = vino_s_crop,
3876 .vidioc_g_crop = vino_g_crop,
3877 .vidioc_s_parm = vino_s_parm,
3878 .vidioc_g_parm = vino_g_parm,
3879 .vidioc_reqbufs = vino_reqbufs,
3880 .vidioc_querybuf = vino_querybuf,
3881 .vidioc_qbuf = vino_qbuf,
3882 .vidioc_dqbuf = vino_dqbuf,
3883 .vidioc_streamon = vino_streamon,
3884 .vidioc_streamoff = vino_streamoff,
3885 .vidioc_queryctrl = vino_queryctrl,
3886 .vidioc_g_ctrl = vino_g_ctrl,
3887 .vidioc_s_ctrl = vino_s_ctrl,
3890 static const struct v4l2_file_operations vino_fops = {
3891 .owner = THIS_MODULE,
3892 .open = vino_open,
3893 .release = vino_close,
3894 .unlocked_ioctl = vino_ioctl,
3895 .mmap = vino_mmap,
3896 .poll = vino_poll,
3899 static struct video_device vdev_template = {
3900 .name = "NOT SET",
3901 .fops = &vino_fops,
3902 .ioctl_ops = &vino_ioctl_ops,
3903 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3906 static void vino_module_cleanup(int stage)
3908 switch(stage) {
3909 case 11:
3910 video_unregister_device(vino_drvdata->b.vdev);
3911 vino_drvdata->b.vdev = NULL;
3912 case 10:
3913 video_unregister_device(vino_drvdata->a.vdev);
3914 vino_drvdata->a.vdev = NULL;
3915 case 9:
3916 i2c_del_adapter(&vino_i2c_adapter);
3917 case 8:
3918 free_irq(SGI_VINO_IRQ, NULL);
3919 case 7:
3920 if (vino_drvdata->b.vdev) {
3921 video_device_release(vino_drvdata->b.vdev);
3922 vino_drvdata->b.vdev = NULL;
3924 case 6:
3925 if (vino_drvdata->a.vdev) {
3926 video_device_release(vino_drvdata->a.vdev);
3927 vino_drvdata->a.vdev = NULL;
3929 case 5:
3930 /* all entries in dma_cpu dummy table have the same address */
3931 dma_unmap_single(NULL,
3932 vino_drvdata->dummy_desc_table.dma_cpu[0],
3933 PAGE_SIZE, DMA_FROM_DEVICE);
3934 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
3935 * sizeof(dma_addr_t),
3936 (void *)vino_drvdata->
3937 dummy_desc_table.dma_cpu,
3938 vino_drvdata->dummy_desc_table.dma);
3939 case 4:
3940 free_page(vino_drvdata->dummy_page);
3941 case 3:
3942 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
3943 case 2:
3944 kfree(vino_drvdata);
3945 case 1:
3946 iounmap(vino);
3947 case 0:
3948 break;
3949 default:
3950 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
3951 stage);
3955 static int vino_probe(void)
3957 unsigned long rev_id;
3959 if (ip22_is_fullhouse()) {
3960 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
3961 return -ENODEV;
3964 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
3965 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
3966 return -ENODEV;
3969 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
3970 if (!vino) {
3971 printk(KERN_ERR "VINO: ioremap() failed\n");
3972 return -EIO;
3974 vino_init_stage++;
3976 if (get_dbe(rev_id, &(vino->rev_id))) {
3977 printk(KERN_ERR "Failed to read VINO revision register\n");
3978 vino_module_cleanup(vino_init_stage);
3979 return -ENODEV;
3982 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
3983 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
3984 rev_id);
3985 vino_module_cleanup(vino_init_stage);
3986 return -ENODEV;
3989 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
3991 return 0;
3994 static int vino_init(void)
3996 dma_addr_t dma_dummy_address;
3997 int err;
3998 int i;
4000 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
4001 if (!vino_drvdata) {
4002 vino_module_cleanup(vino_init_stage);
4003 return -ENOMEM;
4005 vino_init_stage++;
4006 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4007 sizeof(vino_drvdata->v4l2_dev.name));
4008 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4009 if (err)
4010 return err;
4011 vino_init_stage++;
4013 /* create a dummy dma descriptor */
4014 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4015 if (!vino_drvdata->dummy_page) {
4016 vino_module_cleanup(vino_init_stage);
4017 return -ENOMEM;
4019 vino_init_stage++;
4021 // TODO: use page_count in dummy_desc_table
4023 vino_drvdata->dummy_desc_table.dma_cpu =
4024 dma_alloc_coherent(NULL,
4025 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4026 &vino_drvdata->dummy_desc_table.dma,
4027 GFP_KERNEL | GFP_DMA);
4028 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4029 vino_module_cleanup(vino_init_stage);
4030 return -ENOMEM;
4032 vino_init_stage++;
4034 dma_dummy_address = dma_map_single(NULL,
4035 (void *)vino_drvdata->dummy_page,
4036 PAGE_SIZE, DMA_FROM_DEVICE);
4037 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4038 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4041 /* initialize VINO */
4043 vino->control = 0;
4044 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4045 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4046 udelay(VINO_DESC_FETCH_DELAY);
4048 vino->intr_status = 0;
4050 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4051 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4053 return 0;
4056 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4057 unsigned int channel, const char *name)
4059 vcs->channel = channel;
4060 vcs->input = VINO_INPUT_NONE;
4061 vcs->alpha = 0;
4062 vcs->users = 0;
4063 vcs->data_format = VINO_DATA_FMT_GREY;
4064 vcs->data_norm = VINO_DATA_NORM_NTSC;
4065 vcs->decimation = 1;
4066 vino_set_default_clipping(vcs);
4067 vino_set_default_framerate(vcs);
4069 vcs->capturing = 0;
4071 mutex_init(&vcs->mutex);
4072 spin_lock_init(&vcs->capture_lock);
4074 mutex_init(&vcs->fb_queue.queue_mutex);
4075 spin_lock_init(&vcs->fb_queue.queue_lock);
4076 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4078 vcs->vdev = video_device_alloc();
4079 if (!vcs->vdev) {
4080 vino_module_cleanup(vino_init_stage);
4081 return -ENOMEM;
4083 vino_init_stage++;
4085 memcpy(vcs->vdev, &vdev_template,
4086 sizeof(struct video_device));
4087 strcpy(vcs->vdev->name, name);
4088 vcs->vdev->release = video_device_release;
4089 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
4091 video_set_drvdata(vcs->vdev, vcs);
4093 return 0;
4096 static int __init vino_module_init(void)
4098 int ret;
4100 printk(KERN_INFO "SGI VINO driver version %s\n",
4101 VINO_MODULE_VERSION);
4103 ret = vino_probe();
4104 if (ret)
4105 return ret;
4107 ret = vino_init();
4108 if (ret)
4109 return ret;
4111 /* initialize data structures */
4113 spin_lock_init(&vino_drvdata->vino_lock);
4114 spin_lock_init(&vino_drvdata->input_lock);
4116 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4117 vino_vdev_name_a);
4118 if (ret)
4119 return ret;
4121 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4122 vino_vdev_name_b);
4123 if (ret)
4124 return ret;
4126 /* initialize hardware and register V4L devices */
4128 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4129 vino_driver_description, NULL);
4130 if (ret) {
4131 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4132 SGI_VINO_IRQ);
4133 vino_module_cleanup(vino_init_stage);
4134 return -EAGAIN;
4136 vino_init_stage++;
4138 ret = i2c_add_adapter(&vino_i2c_adapter);
4139 if (ret) {
4140 printk(KERN_ERR "VINO I2C bus registration failed\n");
4141 vino_module_cleanup(vino_init_stage);
4142 return ret;
4144 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
4145 vino_init_stage++;
4147 ret = video_register_device(vino_drvdata->a.vdev,
4148 VFL_TYPE_GRABBER, -1);
4149 if (ret < 0) {
4150 printk(KERN_ERR "VINO channel A Video4Linux-device "
4151 "registration failed\n");
4152 vino_module_cleanup(vino_init_stage);
4153 return -EINVAL;
4155 vino_init_stage++;
4157 ret = video_register_device(vino_drvdata->b.vdev,
4158 VFL_TYPE_GRABBER, -1);
4159 if (ret < 0) {
4160 printk(KERN_ERR "VINO channel B Video4Linux-device "
4161 "registration failed\n");
4162 vino_module_cleanup(vino_init_stage);
4163 return -EINVAL;
4165 vino_init_stage++;
4167 vino_drvdata->decoder =
4168 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
4169 "saa7191", "saa7191", 0, I2C_ADDRS(0x45));
4170 vino_drvdata->camera =
4171 v4l2_i2c_new_subdev(&vino_drvdata->v4l2_dev, &vino_i2c_adapter,
4172 "indycam", "indycam", 0, I2C_ADDRS(0x2b));
4174 dprintk("init complete!\n");
4176 return 0;
4179 static void __exit vino_module_exit(void)
4181 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4182 vino_module_cleanup(vino_init_stage);
4183 dprintk("cleanup complete, exit!\n");
4186 module_init(vino_module_init);
4187 module_exit(vino_module_exit);