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>
15 * - remove "mark pages reserved-hacks" from memory allocation code
16 * and implement fault()
17 * - check decimation, calculating and reporting image size when
19 * - implement read(), user mode buffers and overlay (?)
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
31 #include <linux/time.h>
32 #include <linux/version.h>
33 #include <linux/kmod.h>
35 #include <linux/i2c.h>
37 #include <linux/videodev2.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <linux/mutex.h>
42 #include <asm/paccess.h>
44 #include <asm/sgi/ip22.h>
45 #include <asm/sgi/mc.h>
51 /* Uncomment the following line to get lots and lots of (mostly useless)
53 * Note that the debug output also slows down the driver significantly */
55 // #define VINO_DEBUG_INT
57 #define VINO_MODULE_VERSION "0.0.5"
58 #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
60 MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
61 MODULE_VERSION(VINO_MODULE_VERSION);
62 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
63 MODULE_LICENSE("GPL");
66 #define dprintk(x...) printk("VINO: " x);
71 #define VINO_NO_CHANNEL 0
72 #define VINO_CHANNEL_A 1
73 #define VINO_CHANNEL_B 2
75 #define VINO_PAL_WIDTH 768
76 #define VINO_PAL_HEIGHT 576
77 #define VINO_NTSC_WIDTH 640
78 #define VINO_NTSC_HEIGHT 480
80 #define VINO_MIN_WIDTH 32
81 #define VINO_MIN_HEIGHT 32
83 #define VINO_CLIPPING_START_ODD_D1 1
84 #define VINO_CLIPPING_START_ODD_PAL 15
85 #define VINO_CLIPPING_START_ODD_NTSC 12
87 #define VINO_CLIPPING_START_EVEN_D1 2
88 #define VINO_CLIPPING_START_EVEN_PAL 15
89 #define VINO_CLIPPING_START_EVEN_NTSC 12
91 #define VINO_INPUT_CHANNEL_COUNT 3
93 /* the number is the index for vino_inputs */
94 #define VINO_INPUT_NONE -1
95 #define VINO_INPUT_COMPOSITE 0
96 #define VINO_INPUT_SVIDEO 1
97 #define VINO_INPUT_D1 2
99 #define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
101 #define VINO_FIFO_THRESHOLD_DEFAULT 16
103 #define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
104 * VINO_PAL_HEIGHT * 4 \
105 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
107 #define VINO_FRAMEBUFFER_COUNT_MAX 8
109 #define VINO_FRAMEBUFFER_UNUSED 0
110 #define VINO_FRAMEBUFFER_IN_USE 1
111 #define VINO_FRAMEBUFFER_READY 2
113 #define VINO_QUEUE_ERROR -1
114 #define VINO_QUEUE_MAGIC 0x20050125
116 #define VINO_MEMORY_NONE 0
117 #define VINO_MEMORY_MMAP 1
118 #define VINO_MEMORY_USERPTR 2
120 #define VINO_DUMMY_DESC_COUNT 4
121 #define VINO_DESC_FETCH_DELAY 5 /* microseconds */
123 #define VINO_MAX_FRAME_SKIP_COUNT 128
125 /* the number is the index for vino_data_formats */
126 #define VINO_DATA_FMT_NONE -1
127 #define VINO_DATA_FMT_GREY 0
128 #define VINO_DATA_FMT_RGB332 1
129 #define VINO_DATA_FMT_RGB32 2
130 #define VINO_DATA_FMT_YUV 3
132 #define VINO_DATA_FMT_COUNT 4
134 /* the number is the index for vino_data_norms */
135 #define VINO_DATA_NORM_NONE -1
136 #define VINO_DATA_NORM_NTSC 0
137 #define VINO_DATA_NORM_PAL 1
138 #define VINO_DATA_NORM_SECAM 2
139 #define VINO_DATA_NORM_D1 3
141 #define VINO_DATA_NORM_COUNT 4
143 /* I2C controller flags */
144 #define SGI_I2C_FORCE_IDLE (0 << 0)
145 #define SGI_I2C_NOT_IDLE (1 << 0)
146 #define SGI_I2C_WRITE (0 << 1)
147 #define SGI_I2C_READ (1 << 1)
148 #define SGI_I2C_RELEASE_BUS (0 << 2)
149 #define SGI_I2C_HOLD_BUS (1 << 2)
150 #define SGI_I2C_XFER_DONE (0 << 4)
151 #define SGI_I2C_XFER_BUSY (1 << 4)
152 #define SGI_I2C_ACK (0 << 5)
153 #define SGI_I2C_NACK (1 << 5)
154 #define SGI_I2C_BUS_OK (0 << 7)
155 #define SGI_I2C_BUS_ERR (1 << 7)
157 /* Internal data structure definitions */
164 struct vino_clipping {
165 unsigned int left, right, top, bottom;
168 struct vino_data_format {
169 /* the description */
171 /* bytes per pixel */
173 /* V4L2 fourcc code */
175 /* V4L2 colorspace (duh!) */
176 enum v4l2_colorspace colorspace;
179 struct vino_data_norm {
181 unsigned int width, height;
182 struct vino_clipping odd;
183 struct vino_clipping even;
186 unsigned int fps_min, fps_max;
190 struct vino_descriptor_table {
191 /* the number of PAGE_SIZE sized pages in the buffer */
192 unsigned int page_count;
193 /* virtual (kmalloc'd) pointers to the actual data
194 * (in PAGE_SIZE chunks, used with mmap streaming) */
195 unsigned long *virtual;
197 /* cpu address for the VINO descriptor table
198 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
199 unsigned long *dma_cpu;
200 /* dma address for the VINO descriptor table
201 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
205 struct vino_framebuffer {
206 /* identifier nubmer */
208 /* the length of the whole buffer */
210 /* the length of actual data in buffer */
211 unsigned int data_size;
212 /* the data format */
213 unsigned int data_format;
214 /* the state of buffer data */
216 /* is the buffer mapped in user space? */
217 unsigned int map_count;
218 /* memory offset for mmap() */
221 unsigned int frame_counter;
222 /* timestamp (written when image capture finishes) */
223 struct timeval timestamp;
225 struct vino_descriptor_table desc_table;
227 spinlock_t state_lock;
230 struct vino_framebuffer_fifo {
237 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
240 struct vino_framebuffer_queue {
243 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
247 /* data field of in and out contain index numbers for buffer */
248 struct vino_framebuffer_fifo in;
249 struct vino_framebuffer_fifo out;
251 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
253 spinlock_t queue_lock;
254 struct mutex queue_mutex;
255 wait_queue_head_t frame_wait_queue;
258 struct vino_interrupt_data {
259 struct timeval timestamp;
260 unsigned int frame_counter;
261 unsigned int skip_count;
265 struct vino_channel_settings {
266 unsigned int channel;
269 unsigned int data_format;
270 unsigned int data_norm;
271 struct vino_clipping clipping;
272 unsigned int decimation;
273 unsigned int line_size;
276 unsigned int framert_reg;
278 unsigned int fifo_threshold;
280 struct vino_framebuffer_queue fb_queue;
282 /* number of the current field */
285 /* read in progress */
287 /* streaming is active */
289 /* the driver is currently processing the queue */
293 spinlock_t capture_lock;
297 struct vino_interrupt_data int_data;
300 struct video_device *vdev;
303 struct vino_settings {
304 struct v4l2_device v4l2_dev;
305 struct vino_channel_settings a;
306 struct vino_channel_settings b;
308 /* the channel which owns this client:
309 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
310 unsigned int decoder_owner;
311 struct v4l2_subdev *decoder;
312 unsigned int camera_owner;
313 struct v4l2_subdev *camera;
315 /* a lock for vino register access */
316 spinlock_t vino_lock;
317 /* a lock for channel input changes */
318 spinlock_t input_lock;
320 unsigned long dummy_page;
321 struct vino_descriptor_table dummy_desc_table;
324 /* Module parameters */
327 * Using vino_pixel_conversion the ABGR32-format pixels supplied
328 * by the VINO chip can be converted to more common formats
329 * like RGBA32 (or probably RGB24 in the future). This way we
330 * can give out data that can be specified correctly with
331 * the V4L2-definitions.
333 * The pixel format is specified as RGBA32 when no conversion
336 * Note that this only affects the 32-bit bit depth.
338 * Use non-zero value to enable conversion.
340 static int vino_pixel_conversion;
342 module_param_named(pixelconv, vino_pixel_conversion, int, 0);
344 MODULE_PARM_DESC(pixelconv,
345 "enable pixel conversion (non-zero value enables)");
347 /* Internal data structures */
349 static struct sgi_vino *vino;
351 static struct vino_settings *vino_drvdata;
353 #define camera_call(o, f, args...) \
354 v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
355 #define decoder_call(o, f, args...) \
356 v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
358 static const char *vino_driver_name = "vino";
359 static const char *vino_driver_description = "SGI VINO";
360 static const char *vino_bus_name = "GIO64 bus";
361 static const char *vino_vdev_name_a = "SGI VINO Channel A";
362 static const char *vino_vdev_name_b = "SGI VINO Channel B";
364 static void vino_capture_tasklet(unsigned long channel);
366 DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
367 DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
369 static const struct vino_input vino_inputs[] = {
372 .std = V4L2_STD_NTSC | V4L2_STD_PAL
376 .std = V4L2_STD_NTSC | V4L2_STD_PAL
379 .name = "D1/IndyCam",
380 .std = V4L2_STD_NTSC,
384 static const struct vino_data_format vino_data_formats[] = {
386 .description = "8-bit greyscale",
388 .pixelformat = V4L2_PIX_FMT_GREY,
389 .colorspace = V4L2_COLORSPACE_SMPTE170M,
391 .description = "8-bit dithered RGB 3-3-2",
393 .pixelformat = V4L2_PIX_FMT_RGB332,
394 .colorspace = V4L2_COLORSPACE_SRGB,
396 .description = "32-bit RGB",
398 .pixelformat = V4L2_PIX_FMT_RGB32,
399 .colorspace = V4L2_COLORSPACE_SRGB,
401 .description = "YUV 4:2:2",
403 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
404 .colorspace = V4L2_COLORSPACE_SMPTE170M,
408 static const struct vino_data_norm vino_data_norms[] = {
410 .description = "NTSC",
411 .std = V4L2_STD_NTSC,
415 .width = VINO_NTSC_WIDTH,
416 .height = VINO_NTSC_HEIGHT,
418 .top = VINO_CLIPPING_START_ODD_NTSC,
420 .bottom = VINO_CLIPPING_START_ODD_NTSC
421 + VINO_NTSC_HEIGHT / 2 - 1,
422 .right = VINO_NTSC_WIDTH,
425 .top = VINO_CLIPPING_START_EVEN_NTSC,
427 .bottom = VINO_CLIPPING_START_EVEN_NTSC
428 + VINO_NTSC_HEIGHT / 2 - 1,
429 .right = VINO_NTSC_WIDTH,
432 .description = "PAL",
437 .width = VINO_PAL_WIDTH,
438 .height = VINO_PAL_HEIGHT,
440 .top = VINO_CLIPPING_START_ODD_PAL,
442 .bottom = VINO_CLIPPING_START_ODD_PAL
443 + VINO_PAL_HEIGHT / 2 - 1,
444 .right = VINO_PAL_WIDTH,
447 .top = VINO_CLIPPING_START_EVEN_PAL,
449 .bottom = VINO_CLIPPING_START_EVEN_PAL
450 + VINO_PAL_HEIGHT / 2 - 1,
451 .right = VINO_PAL_WIDTH,
454 .description = "SECAM",
455 .std = V4L2_STD_SECAM,
459 .width = VINO_PAL_WIDTH,
460 .height = VINO_PAL_HEIGHT,
462 .top = VINO_CLIPPING_START_ODD_PAL,
464 .bottom = VINO_CLIPPING_START_ODD_PAL
465 + VINO_PAL_HEIGHT / 2 - 1,
466 .right = VINO_PAL_WIDTH,
469 .top = VINO_CLIPPING_START_EVEN_PAL,
471 .bottom = VINO_CLIPPING_START_EVEN_PAL
472 + VINO_PAL_HEIGHT / 2 - 1,
473 .right = VINO_PAL_WIDTH,
476 .description = "NTSC/D1",
477 .std = V4L2_STD_NTSC,
481 .width = VINO_NTSC_WIDTH,
482 .height = VINO_NTSC_HEIGHT,
484 .top = VINO_CLIPPING_START_ODD_D1,
486 .bottom = VINO_CLIPPING_START_ODD_D1
487 + VINO_NTSC_HEIGHT / 2 - 1,
488 .right = VINO_NTSC_WIDTH,
491 .top = VINO_CLIPPING_START_EVEN_D1,
493 .bottom = VINO_CLIPPING_START_EVEN_D1
494 + VINO_NTSC_HEIGHT / 2 - 1,
495 .right = VINO_NTSC_WIDTH,
500 #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
502 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
504 .id = V4L2_CID_AUTOGAIN,
505 .type = V4L2_CTRL_TYPE_BOOLEAN,
506 .name = "Automatic Gain Control",
510 .default_value = INDYCAM_AGC_DEFAULT,
512 .id = V4L2_CID_AUTO_WHITE_BALANCE,
513 .type = V4L2_CTRL_TYPE_BOOLEAN,
514 .name = "Automatic White Balance",
518 .default_value = INDYCAM_AWB_DEFAULT,
521 .type = V4L2_CTRL_TYPE_INTEGER,
523 .minimum = INDYCAM_GAIN_MIN,
524 .maximum = INDYCAM_GAIN_MAX,
526 .default_value = INDYCAM_GAIN_DEFAULT,
528 .id = INDYCAM_CONTROL_RED_SATURATION,
529 .type = V4L2_CTRL_TYPE_INTEGER,
530 .name = "Red Saturation",
531 .minimum = INDYCAM_RED_SATURATION_MIN,
532 .maximum = INDYCAM_RED_SATURATION_MAX,
534 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
536 .id = INDYCAM_CONTROL_BLUE_SATURATION,
537 .type = V4L2_CTRL_TYPE_INTEGER,
538 .name = "Blue Saturation",
539 .minimum = INDYCAM_BLUE_SATURATION_MIN,
540 .maximum = INDYCAM_BLUE_SATURATION_MAX,
542 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
544 .id = V4L2_CID_RED_BALANCE,
545 .type = V4L2_CTRL_TYPE_INTEGER,
546 .name = "Red Balance",
547 .minimum = INDYCAM_RED_BALANCE_MIN,
548 .maximum = INDYCAM_RED_BALANCE_MAX,
550 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
552 .id = V4L2_CID_BLUE_BALANCE,
553 .type = V4L2_CTRL_TYPE_INTEGER,
554 .name = "Blue Balance",
555 .minimum = INDYCAM_BLUE_BALANCE_MIN,
556 .maximum = INDYCAM_BLUE_BALANCE_MAX,
558 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
560 .id = V4L2_CID_EXPOSURE,
561 .type = V4L2_CTRL_TYPE_INTEGER,
562 .name = "Shutter Control",
563 .minimum = INDYCAM_SHUTTER_MIN,
564 .maximum = INDYCAM_SHUTTER_MAX,
566 .default_value = INDYCAM_SHUTTER_DEFAULT,
568 .id = V4L2_CID_GAMMA,
569 .type = V4L2_CTRL_TYPE_INTEGER,
571 .minimum = INDYCAM_GAMMA_MIN,
572 .maximum = INDYCAM_GAMMA_MAX,
574 .default_value = INDYCAM_GAMMA_DEFAULT,
578 #define VINO_SAA7191_V4L2_CONTROL_COUNT 9
580 struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
583 .type = V4L2_CTRL_TYPE_INTEGER,
585 .minimum = SAA7191_HUE_MIN,
586 .maximum = SAA7191_HUE_MAX,
588 .default_value = SAA7191_HUE_DEFAULT,
590 .id = SAA7191_CONTROL_BANDPASS,
591 .type = V4L2_CTRL_TYPE_INTEGER,
592 .name = "Luminance Bandpass",
593 .minimum = SAA7191_BANDPASS_MIN,
594 .maximum = SAA7191_BANDPASS_MAX,
596 .default_value = SAA7191_BANDPASS_DEFAULT,
598 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
599 .type = V4L2_CTRL_TYPE_INTEGER,
600 .name = "Luminance Bandpass Weight",
601 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
602 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
604 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
606 .id = SAA7191_CONTROL_CORING,
607 .type = V4L2_CTRL_TYPE_INTEGER,
608 .name = "HF Luminance Coring",
609 .minimum = SAA7191_CORING_MIN,
610 .maximum = SAA7191_CORING_MAX,
612 .default_value = SAA7191_CORING_DEFAULT,
614 .id = SAA7191_CONTROL_FORCE_COLOUR,
615 .type = V4L2_CTRL_TYPE_BOOLEAN,
616 .name = "Force Colour",
617 .minimum = SAA7191_FORCE_COLOUR_MIN,
618 .maximum = SAA7191_FORCE_COLOUR_MAX,
620 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
622 .id = SAA7191_CONTROL_CHROMA_GAIN,
623 .type = V4L2_CTRL_TYPE_INTEGER,
624 .name = "Chrominance Gain Control",
625 .minimum = SAA7191_CHROMA_GAIN_MIN,
626 .maximum = SAA7191_CHROMA_GAIN_MAX,
628 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
630 .id = SAA7191_CONTROL_VTRC,
631 .type = V4L2_CTRL_TYPE_BOOLEAN,
632 .name = "VTR Time Constant",
633 .minimum = SAA7191_VTRC_MIN,
634 .maximum = SAA7191_VTRC_MAX,
636 .default_value = SAA7191_VTRC_DEFAULT,
638 .id = SAA7191_CONTROL_LUMA_DELAY,
639 .type = V4L2_CTRL_TYPE_INTEGER,
640 .name = "Luminance Delay Compensation",
641 .minimum = SAA7191_LUMA_DELAY_MIN,
642 .maximum = SAA7191_LUMA_DELAY_MAX,
644 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
646 .id = SAA7191_CONTROL_VNR,
647 .type = V4L2_CTRL_TYPE_INTEGER,
648 .name = "Vertical Noise Reduction",
649 .minimum = SAA7191_VNR_MIN,
650 .maximum = SAA7191_VNR_MAX,
652 .default_value = SAA7191_VNR_DEFAULT,
656 /* VINO framebuffer/DMA descriptor management */
658 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
663 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
665 for (i = 0; i < count; i++) {
666 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
667 dma_unmap_single(NULL,
668 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
669 PAGE_SIZE, DMA_FROM_DEVICE);
670 free_page(fb->desc_table.virtual[i]);
673 dma_free_coherent(NULL,
674 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
675 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
677 kfree(fb->desc_table.virtual);
679 memset(fb, 0, sizeof(struct vino_framebuffer));
682 static void vino_free_buffer(struct vino_framebuffer *fb)
684 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
687 static int vino_allocate_buffer(struct vino_framebuffer *fb,
690 unsigned int count, i, j;
693 dprintk("vino_allocate_buffer():\n");
698 memset(fb, 0, sizeof(struct vino_framebuffer));
700 count = ((size / PAGE_SIZE) + 4) & ~3;
702 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
705 /* allocate memory for table with virtual (page) addresses */
706 fb->desc_table.virtual = (unsigned long *)
707 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
708 if (!fb->desc_table.virtual)
711 /* allocate memory for table with dma addresses
712 * (has space for four extra descriptors) */
713 fb->desc_table.dma_cpu =
714 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
715 sizeof(dma_addr_t), &fb->desc_table.dma,
716 GFP_KERNEL | GFP_DMA);
717 if (!fb->desc_table.dma_cpu) {
719 goto out_free_virtual;
722 /* allocate pages for the buffer and acquire the according
724 for (i = 0; i < count; i++) {
725 dma_addr_t dma_data_addr;
727 fb->desc_table.virtual[i] =
728 get_zeroed_page(GFP_KERNEL | GFP_DMA);
729 if (!fb->desc_table.virtual[i]) {
736 (void *)fb->desc_table.virtual[i],
737 PAGE_SIZE, DMA_FROM_DEVICE);
739 for (j = 0; j < VINO_PAGE_RATIO; j++) {
740 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
741 dma_data_addr + VINO_PAGE_SIZE * j;
744 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
747 /* page_count needs to be set anyway, because the descriptor table has
748 * been allocated according to this number */
749 fb->desc_table.page_count = count;
752 /* the descriptor with index i doesn't contain
753 * a valid address yet */
754 vino_free_buffer_with_count(fb, i);
759 fb->size = count * PAGE_SIZE;
760 fb->data_format = VINO_DATA_FMT_NONE;
762 /* set the dma stop-bit for the last (count+1)th descriptor */
763 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
767 kfree(fb->desc_table.virtual);
772 /* user buffers not fully implemented yet */
773 static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
777 unsigned int count, i, j;
780 dprintk("vino_prepare_user_buffer():\n");
785 memset(fb, 0, sizeof(struct vino_framebuffer));
787 count = ((size / PAGE_SIZE)) & ~3;
789 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
792 /* allocate memory for table with virtual (page) addresses */
793 fb->desc_table.virtual = (unsigned long *)
794 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
795 if (!fb->desc_table.virtual)
798 /* allocate memory for table with dma addresses
799 * (has space for four extra descriptors) */
800 fb->desc_table.dma_cpu =
801 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
802 sizeof(dma_addr_t), &fb->desc_table.dma,
803 GFP_KERNEL | GFP_DMA);
804 if (!fb->desc_table.dma_cpu) {
806 goto out_free_virtual;
809 /* allocate pages for the buffer and acquire the according
811 for (i = 0; i < count; i++) {
812 dma_addr_t dma_data_addr;
814 fb->desc_table.virtual[i] =
815 get_zeroed_page(GFP_KERNEL | GFP_DMA);
816 if (!fb->desc_table.virtual[i]) {
823 (void *)fb->desc_table.virtual[i],
824 PAGE_SIZE, DMA_FROM_DEVICE);
826 for (j = 0; j < VINO_PAGE_RATIO; j++) {
827 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
828 dma_data_addr + VINO_PAGE_SIZE * j;
831 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
834 /* page_count needs to be set anyway, because the descriptor table has
835 * been allocated according to this number */
836 fb->desc_table.page_count = count;
839 /* the descriptor with index i doesn't contain
840 * a valid address yet */
841 vino_free_buffer_with_count(fb, i);
846 fb->size = count * PAGE_SIZE;
848 /* set the dma stop-bit for the last (count+1)th descriptor */
849 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
853 kfree(fb->desc_table.virtual);
858 static void vino_sync_buffer(struct vino_framebuffer *fb)
862 dprintk("vino_sync_buffer():\n");
864 for (i = 0; i < fb->desc_table.page_count; i++)
865 dma_sync_single(NULL,
866 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
867 PAGE_SIZE, DMA_FROM_DEVICE);
870 /* Framebuffer fifo functions (need to be locked externally) */
872 static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
880 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
881 length = VINO_FRAMEBUFFER_COUNT_MAX;
886 /* returns true/false */
887 static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
892 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
893 if (f->data[i] == id)
901 /* returns true/false */
902 static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
904 return (f->used == f->length);
908 static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
913 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
915 if (id >= f->length) {
916 return VINO_QUEUE_ERROR;
919 if (vino_fifo_has_id(f, id)) {
920 return VINO_QUEUE_ERROR;
923 if (f->used < f->length) {
924 f->data[f->tail] = id;
925 f->tail = (f->tail + 1) % f->length;
928 return VINO_QUEUE_ERROR;
934 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
937 *id = f->data[f->head];
939 return VINO_QUEUE_ERROR;
945 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
948 *id = f->data[f->head];
949 f->head = (f->head + 1) % f->length;
952 return VINO_QUEUE_ERROR;
958 /* Framebuffer queue functions */
960 /* execute with queue_lock locked */
961 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
967 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
968 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
969 for (i = 0; i < length; i++) {
970 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
972 vino_free_buffer(q->buffer[i]);
976 q->type = VINO_MEMORY_NONE;
980 static void vino_queue_free(struct vino_framebuffer_queue *q)
982 dprintk("vino_queue_free():\n");
984 if (q->magic != VINO_QUEUE_MAGIC)
986 if (q->type != VINO_MEMORY_MMAP)
989 mutex_lock(&q->queue_mutex);
991 vino_queue_free_with_count(q, q->length);
993 mutex_unlock(&q->queue_mutex);
996 static int vino_queue_init(struct vino_framebuffer_queue *q,
997 unsigned int *length)
1002 dprintk("vino_queue_init(): length = %d\n", *length);
1004 if (q->magic == VINO_QUEUE_MAGIC) {
1005 dprintk("vino_queue_init(): queue already initialized!\n");
1009 if (q->type != VINO_MEMORY_NONE) {
1010 dprintk("vino_queue_init(): queue already initialized!\n");
1017 mutex_lock(&q->queue_mutex);
1019 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1020 *length = VINO_FRAMEBUFFER_COUNT_MAX;
1024 for (i = 0; i < *length; i++) {
1025 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1026 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1028 if (!q->buffer[i]) {
1029 dprintk("vino_queue_init(): kmalloc() failed\n");
1034 ret = vino_allocate_buffer(q->buffer[i],
1035 VINO_FRAMEBUFFER_SIZE);
1037 kfree(q->buffer[i]);
1038 dprintk("vino_queue_init(): "
1039 "vino_allocate_buffer() failed\n");
1043 q->buffer[i]->id = i;
1045 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1046 q->buffer[i - 1]->size;
1048 q->buffer[i]->offset = 0;
1051 spin_lock_init(&q->buffer[i]->state_lock);
1053 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1054 "size = %d\n", i, q->buffer[i]->offset,
1055 q->buffer[i]->size);
1059 vino_queue_free_with_count(q, i);
1062 q->length = *length;
1063 vino_fifo_init(&q->in, q->length);
1064 vino_fifo_init(&q->out, q->length);
1065 q->type = VINO_MEMORY_MMAP;
1066 q->magic = VINO_QUEUE_MAGIC;
1069 mutex_unlock(&q->queue_mutex);
1074 static struct vino_framebuffer *vino_queue_add(struct
1075 vino_framebuffer_queue *q,
1078 struct vino_framebuffer *ret = NULL;
1080 unsigned long flags;
1082 dprintk("vino_queue_add(): id = %d\n", id);
1084 if (q->magic != VINO_QUEUE_MAGIC) {
1088 spin_lock_irqsave(&q->queue_lock, flags);
1093 if (id >= q->length)
1096 /* not needed?: if (vino_fifo_full(&q->out)) {
1099 /* check that outgoing queue isn't already full
1100 * (or that it won't become full) */
1101 total = vino_fifo_get_used(&q->in) +
1102 vino_fifo_get_used(&q->out);
1103 if (total >= q->length)
1106 if (vino_fifo_enqueue(&q->in, id))
1109 ret = q->buffer[id];
1112 spin_unlock_irqrestore(&q->queue_lock, flags);
1117 static struct vino_framebuffer *vino_queue_transfer(struct
1118 vino_framebuffer_queue *q)
1120 struct vino_framebuffer *ret = NULL;
1121 struct vino_framebuffer *fb;
1123 unsigned long flags;
1125 dprintk("vino_queue_transfer():\n");
1127 if (q->magic != VINO_QUEUE_MAGIC) {
1131 spin_lock_irqsave(&q->queue_lock, flags);
1136 // now this actually removes an entry from the incoming queue
1137 if (vino_fifo_dequeue(&q->in, &id)) {
1141 dprintk("vino_queue_transfer(): id = %d\n", id);
1144 // we have already checked that the outgoing queue is not full, but...
1145 if (vino_fifo_enqueue(&q->out, id)) {
1146 printk(KERN_ERR "vino_queue_transfer(): "
1147 "outgoing queue is full, this shouldn't happen!\n");
1153 spin_unlock_irqrestore(&q->queue_lock, flags);
1158 /* returns true/false */
1159 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1163 unsigned long flags;
1165 if (q->magic != VINO_QUEUE_MAGIC) {
1169 spin_lock_irqsave(&q->queue_lock, flags);
1174 ret = vino_fifo_has_id(&q->in, id);
1177 spin_unlock_irqrestore(&q->queue_lock, flags);
1182 /* returns true/false */
1183 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1187 unsigned long flags;
1189 if (q->magic != VINO_QUEUE_MAGIC) {
1193 spin_lock_irqsave(&q->queue_lock, flags);
1198 ret = vino_fifo_has_id(&q->out, id);
1201 spin_unlock_irqrestore(&q->queue_lock, flags);
1206 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1210 unsigned long flags;
1212 if (q->magic != VINO_QUEUE_MAGIC) {
1213 return VINO_QUEUE_ERROR;
1216 spin_lock_irqsave(&q->queue_lock, flags);
1218 if (q->length == 0) {
1219 ret = VINO_QUEUE_ERROR;
1223 *used = vino_fifo_get_used(&q->in);
1226 spin_unlock_irqrestore(&q->queue_lock, flags);
1231 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1235 unsigned long flags;
1237 if (q->magic != VINO_QUEUE_MAGIC) {
1238 return VINO_QUEUE_ERROR;
1241 spin_lock_irqsave(&q->queue_lock, flags);
1243 if (q->length == 0) {
1244 ret = VINO_QUEUE_ERROR;
1248 *used = vino_fifo_get_used(&q->out);
1251 spin_unlock_irqrestore(&q->queue_lock, flags);
1257 static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1258 unsigned int *total)
1261 unsigned long flags;
1263 if (q->magic != VINO_QUEUE_MAGIC) {
1264 return VINO_QUEUE_ERROR;
1267 spin_lock_irqsave(&q->queue_lock, flags);
1269 if (q->length == 0) {
1270 ret = VINO_QUEUE_ERROR;
1274 *total = vino_fifo_get_used(&q->in) +
1275 vino_fifo_get_used(&q->out);
1278 spin_unlock_irqrestore(&q->queue_lock, flags);
1284 static struct vino_framebuffer *vino_queue_peek(struct
1285 vino_framebuffer_queue *q,
1288 struct vino_framebuffer *ret = NULL;
1289 unsigned long flags;
1291 if (q->magic != VINO_QUEUE_MAGIC) {
1295 spin_lock_irqsave(&q->queue_lock, flags);
1300 if (vino_fifo_peek(&q->in, id)) {
1304 ret = q->buffer[*id];
1306 spin_unlock_irqrestore(&q->queue_lock, flags);
1311 static struct vino_framebuffer *vino_queue_remove(struct
1312 vino_framebuffer_queue *q,
1315 struct vino_framebuffer *ret = NULL;
1316 unsigned long flags;
1317 dprintk("vino_queue_remove():\n");
1319 if (q->magic != VINO_QUEUE_MAGIC) {
1323 spin_lock_irqsave(&q->queue_lock, flags);
1328 if (vino_fifo_dequeue(&q->out, id)) {
1332 dprintk("vino_queue_remove(): id = %d\n", *id);
1333 ret = q->buffer[*id];
1335 spin_unlock_irqrestore(&q->queue_lock, flags);
1341 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1344 struct vino_framebuffer *ret = NULL;
1345 unsigned long flags;
1347 if (q->magic != VINO_QUEUE_MAGIC) {
1351 spin_lock_irqsave(&q->queue_lock, flags);
1356 if (id >= q->length)
1359 ret = q->buffer[id];
1361 spin_unlock_irqrestore(&q->queue_lock, flags);
1366 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1368 unsigned int length = 0;
1369 unsigned long flags;
1371 if (q->magic != VINO_QUEUE_MAGIC) {
1375 spin_lock_irqsave(&q->queue_lock, flags);
1377 spin_unlock_irqrestore(&q->queue_lock, flags);
1382 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1386 unsigned long flags;
1388 if (q->magic != VINO_QUEUE_MAGIC) {
1392 spin_lock_irqsave(&q->queue_lock, flags);
1393 for (i = 0; i < q->length; i++) {
1394 if (q->buffer[i]->map_count > 0) {
1399 spin_unlock_irqrestore(&q->queue_lock, flags);
1404 /* VINO functions */
1406 /* execute with input_lock locked */
1407 static void vino_update_line_size(struct vino_channel_settings *vcs)
1409 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1410 unsigned int d = vcs->decimation;
1411 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1414 dprintk("update_line_size(): before: w = %d, d = %d, "
1415 "line_size = %d\n", w, d, vcs->line_size);
1417 /* line size must be multiple of 8 bytes */
1418 lsize = (bpp * (w / d)) & ~7;
1419 w = (lsize / bpp) * d;
1421 vcs->clipping.right = vcs->clipping.left + w;
1422 vcs->line_size = lsize;
1424 dprintk("update_line_size(): after: w = %d, d = %d, "
1425 "line_size = %d\n", w, d, vcs->line_size);
1428 /* execute with input_lock locked */
1429 static void vino_set_clipping(struct vino_channel_settings *vcs,
1430 unsigned int x, unsigned int y,
1431 unsigned int w, unsigned int h)
1433 unsigned int maxwidth, maxheight;
1436 maxwidth = vino_data_norms[vcs->data_norm].width;
1437 maxheight = vino_data_norms[vcs->data_norm].height;
1438 d = vcs->decimation;
1440 y &= ~1; /* odd/even fields */
1445 if (y > maxheight) {
1449 if (((w / d) < VINO_MIN_WIDTH)
1450 || ((h / d) < VINO_MIN_HEIGHT)) {
1451 w = VINO_MIN_WIDTH * d;
1452 h = VINO_MIN_HEIGHT * d;
1455 if ((x + w) > maxwidth) {
1457 if ((w / d) < VINO_MIN_WIDTH)
1458 x = maxwidth - VINO_MIN_WIDTH * d;
1460 if ((y + h) > maxheight) {
1462 if ((h / d) < VINO_MIN_HEIGHT)
1463 y = maxheight - VINO_MIN_HEIGHT * d;
1466 vcs->clipping.left = x;
1467 vcs->clipping.top = y;
1468 vcs->clipping.right = x + w;
1469 vcs->clipping.bottom = y + h;
1471 vino_update_line_size(vcs);
1473 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1474 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1475 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1478 /* execute with input_lock locked */
1479 static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
1481 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1482 vino_data_norms[vcs->data_norm].height);
1485 /* execute with input_lock locked */
1486 static void vino_set_scaling(struct vino_channel_settings *vcs,
1487 unsigned int w, unsigned int h)
1489 unsigned int x, y, curw, curh, d;
1491 x = vcs->clipping.left;
1492 y = vcs->clipping.top;
1493 curw = vcs->clipping.right - vcs->clipping.left;
1494 curh = vcs->clipping.bottom - vcs->clipping.top;
1496 d = max(curw / w, curh / h);
1498 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1499 w, h, curw, curh, d);
1507 vcs->decimation = d;
1508 vino_set_clipping(vcs, x, y, w * d, h * d);
1510 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1511 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1512 vcs->decimation, vcs->line_size);
1515 /* execute with input_lock locked */
1516 static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
1518 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1519 vcs->clipping.bottom - vcs->clipping.top);
1522 /* execute with input_lock locked */
1523 static void vino_set_framerate(struct vino_channel_settings *vcs,
1528 switch (vcs->data_norm) {
1529 case VINO_DATA_NORM_NTSC:
1530 case VINO_DATA_NORM_D1:
1531 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1533 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1534 fps = vino_data_norms[vcs->data_norm].fps_min;
1535 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1536 fps = vino_data_norms[vcs->data_norm].fps_max;
1555 mask = VINO_FRAMERT_FULL;
1557 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1559 case VINO_DATA_NORM_PAL:
1560 case VINO_DATA_NORM_SECAM:
1561 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1563 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1564 fps = vino_data_norms[vcs->data_norm].fps_min;
1565 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1566 fps = vino_data_norms[vcs->data_norm].fps_max;
1585 mask = VINO_FRAMERT_FULL;
1587 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1594 /* execute with input_lock locked */
1595 static inline void vino_set_default_framerate(struct
1596 vino_channel_settings *vcs)
1598 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1601 /* VINO I2C bus functions */
1603 struct i2c_algo_sgi_data {
1604 void *data; /* private data for lowlevel routines */
1605 unsigned (*getctrl)(void *data);
1606 void (*setctrl)(void *data, unsigned val);
1607 unsigned (*rdata)(void *data);
1608 void (*wdata)(void *data, unsigned val);
1614 static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1618 for (i = 0; i < adap->xfer_timeout; i++) {
1619 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1627 static int wait_ack(struct i2c_algo_sgi_data *adap)
1631 if (wait_xfer_done(adap))
1633 for (i = 0; i < adap->ack_timeout; i++) {
1634 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1642 static int force_idle(struct i2c_algo_sgi_data *adap)
1646 adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1647 for (i = 0; i < adap->xfer_timeout; i++) {
1648 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1654 if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1659 static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1663 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1664 /* Check if bus is idle, eventually force it to do so */
1665 if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1666 if (force_idle(adap))
1668 /* Write out the i2c chip address and specify operation */
1669 adap->setctrl(adap->data,
1670 SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1673 adap->wdata(adap->data, addr);
1679 static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1684 adap->setctrl(adap->data,
1685 SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1686 for (i = 0; i < len; i++) {
1687 if (wait_xfer_done(adap))
1689 buf[i] = adap->rdata(adap->data);
1691 adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1697 static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1702 /* We are already in write state */
1703 for (i = 0; i < len; i++) {
1704 adap->wdata(adap->data, buf[i]);
1711 static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1714 struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1718 for (i = 0; !err && i < num; i++) {
1720 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1723 if (p->flags & I2C_M_RD)
1724 err = i2c_read(adap, p->buf, p->len);
1726 err = i2c_write(adap, p->buf, p->len);
1729 return (err < 0) ? err : i;
1732 static u32 sgi_func(struct i2c_adapter *adap)
1734 return I2C_FUNC_SMBUS_EMUL;
1737 static const struct i2c_algorithm sgi_algo = {
1738 .master_xfer = sgi_xfer,
1739 .functionality = sgi_func,
1742 static unsigned i2c_vino_getctrl(void *data)
1744 return vino->i2c_control;
1747 static void i2c_vino_setctrl(void *data, unsigned val)
1749 vino->i2c_control = val;
1752 static unsigned i2c_vino_rdata(void *data)
1754 return vino->i2c_data;
1757 static void i2c_vino_wdata(void *data, unsigned val)
1759 vino->i2c_data = val;
1762 static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1763 .getctrl = &i2c_vino_getctrl,
1764 .setctrl = &i2c_vino_setctrl,
1765 .rdata = &i2c_vino_rdata,
1766 .wdata = &i2c_vino_wdata,
1767 .xfer_timeout = 200,
1768 .ack_timeout = 1000,
1771 static struct i2c_adapter vino_i2c_adapter = {
1772 .name = "VINO I2C bus",
1773 .id = I2C_HW_SGI_VINO,
1775 .algo_data = &i2c_sgi_vino_data,
1776 .owner = THIS_MODULE,
1780 * Prepare VINO for DMA transfer...
1781 * (execute only with vino_lock and input_lock locked)
1783 static int vino_dma_setup(struct vino_channel_settings *vcs,
1784 struct vino_framebuffer *fb)
1787 struct sgi_vino_channel *ch;
1788 const struct vino_data_norm *norm;
1790 dprintk("vino_dma_setup():\n");
1793 fb->frame_counter = 0;
1795 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1796 norm = &vino_data_norms[vcs->data_norm];
1801 /* VINO line size register is set 8 bytes less than actual */
1802 ch->line_size = vcs->line_size - 8;
1804 /* let VINO know where to transfer data */
1805 ch->start_desc_tbl = fb->desc_table.dma;
1806 ch->next_4_desc = fb->desc_table.dma;
1808 /* give vino time to fetch the first four descriptors, 5 usec
1809 * should be more than enough time */
1810 udelay(VINO_DESC_FETCH_DELAY);
1812 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1813 ch->start_desc_tbl, ch->next_4_desc);
1815 /* set the alpha register */
1816 ch->alpha = vcs->alpha;
1818 /* set clipping registers */
1819 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1820 VINO_CLIP_EVEN(norm->even.top +
1821 vcs->clipping.top / 2) |
1822 VINO_CLIP_X(vcs->clipping.left);
1823 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1824 vcs->clipping.bottom / 2 - 1) |
1825 VINO_CLIP_EVEN(norm->even.top +
1826 vcs->clipping.bottom / 2 - 1) |
1827 VINO_CLIP_X(vcs->clipping.right);
1829 /* set the size of actual content in the buffer (DECIMATION !) */
1830 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1832 ((vcs->clipping.bottom - vcs->clipping.top) /
1834 vino_data_formats[vcs->data_format].bpp;
1836 ch->frame_rate = vcs->framert_reg;
1838 ctrl = vino->control;
1839 intr = vino->intr_status;
1841 if (vcs->channel == VINO_CHANNEL_A) {
1842 /* All interrupt conditions for this channel was cleared
1843 * so clear the interrupt status register and enable
1845 intr &= ~VINO_INTSTAT_A;
1846 ctrl |= VINO_CTRL_A_INT;
1848 /* enable synchronization */
1849 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1851 /* enable frame assembly */
1852 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1854 /* set decimation used */
1855 if (vcs->decimation < 2)
1856 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1858 ctrl |= VINO_CTRL_A_DEC_ENBL;
1859 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1860 ctrl |= (vcs->decimation - 1) <<
1861 VINO_CTRL_A_DEC_SCALE_SHIFT;
1864 /* select input interface */
1865 if (vcs->input == VINO_INPUT_D1)
1866 ctrl |= VINO_CTRL_A_SELECT;
1868 ctrl &= ~VINO_CTRL_A_SELECT;
1871 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1872 VINO_CTRL_A_DITHER);
1874 intr &= ~VINO_INTSTAT_B;
1875 ctrl |= VINO_CTRL_B_INT;
1877 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1878 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1880 if (vcs->decimation < 2)
1881 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1883 ctrl |= VINO_CTRL_B_DEC_ENBL;
1884 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1885 ctrl |= (vcs->decimation - 1) <<
1886 VINO_CTRL_B_DEC_SCALE_SHIFT;
1889 if (vcs->input == VINO_INPUT_D1)
1890 ctrl |= VINO_CTRL_B_SELECT;
1892 ctrl &= ~VINO_CTRL_B_SELECT;
1894 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1895 VINO_CTRL_B_DITHER);
1899 fb->data_format = vcs->data_format;
1901 switch (vcs->data_format) {
1902 case VINO_DATA_FMT_GREY:
1903 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1904 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1906 case VINO_DATA_FMT_RGB32:
1907 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1908 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1910 case VINO_DATA_FMT_YUV:
1911 /* nothing needs to be done */
1913 case VINO_DATA_FMT_RGB332:
1914 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1915 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1916 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1920 vino->intr_status = intr;
1921 vino->control = ctrl;
1926 /* (execute only with vino_lock locked) */
1927 static inline void vino_dma_start(struct vino_channel_settings *vcs)
1929 u32 ctrl = vino->control;
1931 dprintk("vino_dma_start():\n");
1932 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1933 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1934 vino->control = ctrl;
1937 /* (execute only with vino_lock locked) */
1938 static inline void vino_dma_stop(struct vino_channel_settings *vcs)
1940 u32 ctrl = vino->control;
1942 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1943 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1944 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1945 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
1946 vino->control = ctrl;
1947 dprintk("vino_dma_stop():\n");
1951 * Load dummy page to descriptor registers. This prevents generating of
1952 * spurious interrupts. (execute only with vino_lock locked)
1954 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1956 struct sgi_vino_channel *ch;
1958 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1963 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1964 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1966 udelay(VINO_DESC_FETCH_DELAY);
1967 dprintk("channel %c clear interrupt condition\n",
1968 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1971 static int vino_capture(struct vino_channel_settings *vcs,
1972 struct vino_framebuffer *fb)
1975 unsigned long flags, flags2;
1977 spin_lock_irqsave(&fb->state_lock, flags);
1979 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1981 fb->state = VINO_FRAMEBUFFER_IN_USE;
1983 spin_unlock_irqrestore(&fb->state_lock, flags);
1988 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1989 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1991 vino_dma_setup(vcs, fb);
1992 vino_dma_start(vcs);
1994 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1995 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2001 struct vino_framebuffer *vino_capture_enqueue(struct
2002 vino_channel_settings *vcs,
2005 struct vino_framebuffer *fb;
2006 unsigned long flags;
2008 dprintk("vino_capture_enqueue():\n");
2010 spin_lock_irqsave(&vcs->capture_lock, flags);
2012 fb = vino_queue_add(&vcs->fb_queue, index);
2014 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
2019 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2024 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
2026 struct vino_framebuffer *fb;
2027 unsigned int incoming, id;
2029 unsigned long flags;
2031 dprintk("vino_capture_next():\n");
2033 spin_lock_irqsave(&vcs->capture_lock, flags);
2036 /* start capture only if capture isn't in progress already */
2037 if (vcs->capturing) {
2038 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2043 /* capture next frame:
2044 * stop capture if capturing is not set */
2045 if (!vcs->capturing) {
2046 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2051 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2053 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2058 if (incoming == 0) {
2059 dprintk("vino_capture_next(): no buffers available\n");
2063 fb = vino_queue_peek(&vcs->fb_queue, &id);
2065 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2074 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2076 err = vino_capture(vcs, fb);
2082 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2087 static inline int vino_is_capturing(struct vino_channel_settings *vcs)
2090 unsigned long flags;
2092 spin_lock_irqsave(&vcs->capture_lock, flags);
2094 ret = vcs->capturing;
2096 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2101 /* waits until a frame is captured */
2102 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2107 dprintk("vino_wait_for_frame():\n");
2109 init_waitqueue_entry(&wait, current);
2110 /* add ourselves into wait queue */
2111 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2113 /* to ensure that schedule_timeout will return immediately
2114 * if VINO interrupt was triggered meanwhile */
2115 schedule_timeout_interruptible(msecs_to_jiffies(100));
2117 if (signal_pending(current))
2120 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2122 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2123 err ? "failed" : "ok");
2128 /* the function assumes that PAGE_SIZE % 4 == 0 */
2129 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2130 unsigned char *pageptr;
2131 unsigned int page, i;
2134 for (page = 0; page < fb->desc_table.page_count; page++) {
2135 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2137 for (i = 0; i < PAGE_SIZE; i += 4) {
2139 pageptr[0] = pageptr[3];
2140 pageptr[1] = pageptr[2];
2141 pageptr[2] = pageptr[1];
2148 /* checks if the buffer is in correct state and syncs data */
2149 static int vino_check_buffer(struct vino_channel_settings *vcs,
2150 struct vino_framebuffer *fb)
2153 unsigned long flags;
2155 dprintk("vino_check_buffer():\n");
2157 spin_lock_irqsave(&fb->state_lock, flags);
2158 switch (fb->state) {
2159 case VINO_FRAMEBUFFER_IN_USE:
2162 case VINO_FRAMEBUFFER_READY:
2163 vino_sync_buffer(fb);
2164 fb->state = VINO_FRAMEBUFFER_UNUSED;
2169 spin_unlock_irqrestore(&fb->state_lock, flags);
2172 if (vino_pixel_conversion
2173 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2174 vino_convert_to_rgba(fb);
2176 } else if (err && (err != -EINVAL)) {
2177 dprintk("vino_check_buffer(): buffer not ready\n");
2179 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2181 vino_clear_interrupt(vcs);
2182 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2188 /* forcefully terminates capture */
2189 static void vino_capture_stop(struct vino_channel_settings *vcs)
2191 unsigned int incoming = 0, outgoing = 0, id;
2192 unsigned long flags, flags2;
2194 dprintk("vino_capture_stop():\n");
2196 spin_lock_irqsave(&vcs->capture_lock, flags);
2198 /* unset capturing to stop queue processing */
2201 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2204 vino_clear_interrupt(vcs);
2206 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2208 /* remove all items from the queue */
2209 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2210 dprintk("vino_capture_stop(): "
2211 "vino_queue_get_incoming() failed\n");
2214 while (incoming > 0) {
2215 vino_queue_transfer(&vcs->fb_queue);
2217 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2218 dprintk("vino_capture_stop(): "
2219 "vino_queue_get_incoming() failed\n");
2224 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2225 dprintk("vino_capture_stop(): "
2226 "vino_queue_get_outgoing() failed\n");
2229 while (outgoing > 0) {
2230 vino_queue_remove(&vcs->fb_queue, &id);
2232 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2233 dprintk("vino_capture_stop(): "
2234 "vino_queue_get_outgoing() failed\n");
2240 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2244 static int vino_capture_failed(struct vino_channel_settings *vcs)
2246 struct vino_framebuffer *fb;
2247 unsigned long flags;
2251 dprintk("vino_capture_failed():\n");
2253 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2256 vino_clear_interrupt(vcs);
2258 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2260 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2261 if (ret == VINO_QUEUE_ERROR) {
2262 dprintk("vino_queue_get_incoming() failed\n");
2266 /* no buffers to process */
2270 fb = vino_queue_peek(&vcs->fb_queue, &i);
2272 dprintk("vino_queue_peek() failed\n");
2276 spin_lock_irqsave(&fb->state_lock, flags);
2277 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2278 fb->state = VINO_FRAMEBUFFER_UNUSED;
2279 vino_queue_transfer(&vcs->fb_queue);
2280 vino_queue_remove(&vcs->fb_queue, &i);
2281 /* we should actually discard the newest frame,
2282 * but who cares ... */
2284 spin_unlock_irqrestore(&fb->state_lock, flags);
2290 static void vino_skip_frame(struct vino_channel_settings *vcs)
2292 struct vino_framebuffer *fb;
2293 unsigned long flags;
2296 spin_lock_irqsave(&vcs->capture_lock, flags);
2297 fb = vino_queue_peek(&vcs->fb_queue, &id);
2299 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2300 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2303 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2305 spin_lock_irqsave(&fb->state_lock, flags);
2306 fb->state = VINO_FRAMEBUFFER_UNUSED;
2307 spin_unlock_irqrestore(&fb->state_lock, flags);
2309 vino_capture_next(vcs, 0);
2312 static void vino_frame_done(struct vino_channel_settings *vcs)
2314 struct vino_framebuffer *fb;
2315 unsigned long flags;
2317 spin_lock_irqsave(&vcs->capture_lock, flags);
2318 fb = vino_queue_transfer(&vcs->fb_queue);
2320 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2321 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2324 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2326 fb->frame_counter = vcs->int_data.frame_counter;
2327 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2328 sizeof(struct timeval));
2330 spin_lock_irqsave(&fb->state_lock, flags);
2331 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2332 fb->state = VINO_FRAMEBUFFER_READY;
2333 spin_unlock_irqrestore(&fb->state_lock, flags);
2335 wake_up(&vcs->fb_queue.frame_wait_queue);
2337 vino_capture_next(vcs, 0);
2340 static void vino_capture_tasklet(unsigned long channel) {
2341 struct vino_channel_settings *vcs;
2343 vcs = (channel == VINO_CHANNEL_A)
2344 ? &vino_drvdata->a : &vino_drvdata->b;
2346 if (vcs->int_data.skip)
2347 vcs->int_data.skip_count++;
2349 if (vcs->int_data.skip && (vcs->int_data.skip_count
2350 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2351 vino_skip_frame(vcs);
2353 vcs->int_data.skip_count = 0;
2354 vino_frame_done(vcs);
2358 static irqreturn_t vino_interrupt(int irq, void *dev_id)
2361 unsigned int fc_a, fc_b;
2362 int handled_a = 0, skip_a = 0, done_a = 0;
2363 int handled_b = 0, skip_b = 0, done_b = 0;
2365 #ifdef VINO_DEBUG_INT
2367 unsigned int line_count = vino->a.line_count,
2368 page_index = vino->a.page_index,
2369 field_counter = vino->a.field_counter,
2370 start_desc_tbl = vino->a.start_desc_tbl,
2371 next_4_desc = vino->a.next_4_desc;
2372 unsigned int line_count_2,
2379 spin_lock(&vino_drvdata->vino_lock);
2381 while ((intr = vino->intr_status)) {
2382 fc_a = vino->a.field_counter >> 1;
2383 fc_b = vino->b.field_counter >> 1;
2385 /* handle error-interrupts in some special way ?
2386 * --> skips frames */
2387 if (intr & VINO_INTSTAT_A) {
2388 if (intr & VINO_INTSTAT_A_EOF) {
2389 vino_drvdata->a.field++;
2390 if (vino_drvdata->a.field > 1) {
2391 vino_dma_stop(&vino_drvdata->a);
2392 vino_clear_interrupt(&vino_drvdata->a);
2393 vino_drvdata->a.field = 0;
2396 if (vino->a.page_index
2397 != vino_drvdata->a.line_size) {
2398 vino->a.line_count = 0;
2399 vino->a.page_index =
2402 vino->a.next_4_desc =
2403 vino->a.start_desc_tbl;
2406 dprintk("channel A end-of-field "
2407 "interrupt: %04x\n", intr);
2409 vino_dma_stop(&vino_drvdata->a);
2410 vino_clear_interrupt(&vino_drvdata->a);
2411 vino_drvdata->a.field = 0;
2413 dprintk("channel A error interrupt: %04x\n",
2417 #ifdef VINO_DEBUG_INT
2418 line_count_2 = vino->a.line_count;
2419 page_index_2 = vino->a.page_index;
2420 field_counter_2 = vino->a.field_counter;
2421 start_desc_tbl_2 = vino->a.start_desc_tbl;
2422 next_4_desc_2 = vino->a.next_4_desc;
2424 printk("intr = %04x, loop = %d, field = %d\n",
2425 intr, loop, vino_drvdata->a.field);
2426 printk("1- line count = %04d, page index = %04d, "
2427 "start = %08x, next = %08x\n"
2428 " fieldc = %d, framec = %d\n",
2429 line_count, page_index, start_desc_tbl,
2430 next_4_desc, field_counter, fc_a);
2431 printk("12-line count = %04d, page index = %04d, "
2432 " start = %08x, next = %08x\n",
2433 line_count_2, page_index_2, start_desc_tbl_2,
2441 if (intr & VINO_INTSTAT_B) {
2442 if (intr & VINO_INTSTAT_B_EOF) {
2443 vino_drvdata->b.field++;
2444 if (vino_drvdata->b.field > 1) {
2445 vino_dma_stop(&vino_drvdata->b);
2446 vino_clear_interrupt(&vino_drvdata->b);
2447 vino_drvdata->b.field = 0;
2450 dprintk("channel B end-of-field "
2451 "interrupt: %04x\n", intr);
2453 vino_dma_stop(&vino_drvdata->b);
2454 vino_clear_interrupt(&vino_drvdata->b);
2455 vino_drvdata->b.field = 0;
2457 dprintk("channel B error interrupt: %04x\n",
2462 /* Always remember to clear interrupt status.
2463 * Disable VINO interrupts while we do this. */
2464 ctrl = vino->control;
2465 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2466 vino->intr_status = ~intr;
2467 vino->control = ctrl;
2469 spin_unlock(&vino_drvdata->vino_lock);
2471 if ((!handled_a) && (done_a || skip_a)) {
2473 do_gettimeofday(&vino_drvdata->
2474 a.int_data.timestamp);
2475 vino_drvdata->a.int_data.frame_counter = fc_a;
2477 vino_drvdata->a.int_data.skip = skip_a;
2479 dprintk("channel A %s, interrupt: %d\n",
2480 skip_a ? "skipping frame" : "frame done",
2482 tasklet_hi_schedule(&vino_tasklet_a);
2486 if ((!handled_b) && (done_b || skip_b)) {
2488 do_gettimeofday(&vino_drvdata->
2489 b.int_data.timestamp);
2490 vino_drvdata->b.int_data.frame_counter = fc_b;
2492 vino_drvdata->b.int_data.skip = skip_b;
2494 dprintk("channel B %s, interrupt: %d\n",
2495 skip_b ? "skipping frame" : "frame done",
2497 tasklet_hi_schedule(&vino_tasklet_b);
2501 #ifdef VINO_DEBUG_INT
2504 spin_lock(&vino_drvdata->vino_lock);
2507 spin_unlock(&vino_drvdata->vino_lock);
2512 /* VINO video input management */
2514 static int vino_get_saa7191_input(int input)
2517 case VINO_INPUT_COMPOSITE:
2518 return SAA7191_INPUT_COMPOSITE;
2519 case VINO_INPUT_SVIDEO:
2520 return SAA7191_INPUT_SVIDEO;
2522 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2523 "invalid input!\n");
2528 /* execute with input_lock locked */
2529 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2531 switch(vcs->input) {
2532 case VINO_INPUT_COMPOSITE:
2533 case VINO_INPUT_SVIDEO:
2534 return vino_drvdata->decoder_owner == vcs->channel;
2536 return vino_drvdata->camera_owner == vcs->channel;
2542 static int vino_acquire_input(struct vino_channel_settings *vcs)
2544 unsigned long flags;
2547 dprintk("vino_acquire_input():\n");
2549 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2551 /* First try D1 and then SAA7191 */
2552 if (vino_drvdata->camera
2553 && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2554 vino_drvdata->camera_owner = vcs->channel;
2555 vcs->input = VINO_INPUT_D1;
2556 vcs->data_norm = VINO_DATA_NORM_D1;
2557 } else if (vino_drvdata->decoder
2558 && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
2562 struct v4l2_routing route = { 0, 0 };
2564 input = VINO_INPUT_COMPOSITE;
2566 route.input = vino_get_saa7191_input(input);
2567 ret = decoder_call(video, s_routing, &route);
2573 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2575 /* Don't hold spinlocks while auto-detecting norm
2576 * as it may take a while... */
2578 ret = decoder_call(video, querystd, &norm);
2580 for (data_norm = 0; data_norm < 3; data_norm++) {
2581 if (vino_data_norms[data_norm].std & norm)
2585 data_norm = VINO_DATA_NORM_PAL;
2586 ret = decoder_call(tuner, s_std, norm);
2589 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2596 vino_drvdata->decoder_owner = vcs->channel;
2599 vcs->data_norm = data_norm;
2601 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2602 vino_drvdata->b.input : vino_drvdata->a.input;
2603 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2604 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2607 if (vcs->input == VINO_INPUT_NONE) {
2612 vino_set_default_clipping(vcs);
2613 vino_set_default_scaling(vcs);
2614 vino_set_default_framerate(vcs);
2616 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2619 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2624 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2626 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2627 &vino_drvdata->b : &vino_drvdata->a;
2628 unsigned long flags;
2631 dprintk("vino_set_input():\n");
2633 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2635 if (vcs->input == input)
2639 case VINO_INPUT_COMPOSITE:
2640 case VINO_INPUT_SVIDEO:
2641 if (!vino_drvdata->decoder) {
2646 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2647 vino_drvdata->decoder_owner = vcs->channel;
2650 if (vino_drvdata->decoder_owner == vcs->channel) {
2653 struct v4l2_routing route = { 0, 0 };
2655 route.input = vino_get_saa7191_input(input);
2656 ret = decoder_call(video, s_routing, &route);
2658 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2663 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2665 /* Don't hold spinlocks while auto-detecting norm
2666 * as it may take a while... */
2668 ret = decoder_call(video, querystd, &norm);
2670 for (data_norm = 0; data_norm < 3; data_norm++) {
2671 if (vino_data_norms[data_norm].std & norm)
2675 data_norm = VINO_DATA_NORM_PAL;
2676 ret = decoder_call(tuner, s_std, norm);
2679 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2682 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2688 vcs->data_norm = data_norm;
2690 if (input != vcs2->input) {
2696 vcs->data_norm = vcs2->data_norm;
2699 if (vino_drvdata->camera_owner == vcs->channel) {
2700 /* Transfer the ownership or release the input */
2701 if (vcs2->input == VINO_INPUT_D1) {
2702 vino_drvdata->camera_owner = vcs2->channel;
2704 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2709 if (!vino_drvdata->camera) {
2714 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2715 vino_drvdata->camera_owner = vcs->channel;
2717 if (vino_drvdata->decoder_owner == vcs->channel) {
2718 /* Transfer the ownership or release the input */
2719 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2720 (vcs2->input == VINO_INPUT_SVIDEO)) {
2721 vino_drvdata->decoder_owner = vcs2->channel;
2723 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2728 vcs->data_norm = VINO_DATA_NORM_D1;
2735 vino_set_default_clipping(vcs);
2736 vino_set_default_scaling(vcs);
2737 vino_set_default_framerate(vcs);
2739 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2742 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2747 static void vino_release_input(struct vino_channel_settings *vcs)
2749 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2750 &vino_drvdata->b : &vino_drvdata->a;
2751 unsigned long flags;
2753 dprintk("vino_release_input():\n");
2755 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2757 /* Release ownership of the channel
2758 * and if the other channel takes input from
2759 * the same source, transfer the ownership */
2760 if (vino_drvdata->camera_owner == vcs->channel) {
2761 if (vcs2->input == VINO_INPUT_D1) {
2762 vino_drvdata->camera_owner = vcs2->channel;
2764 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2766 } else if (vino_drvdata->decoder_owner == vcs->channel) {
2767 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2768 (vcs2->input == VINO_INPUT_SVIDEO)) {
2769 vino_drvdata->decoder_owner = vcs2->channel;
2771 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2774 vcs->input = VINO_INPUT_NONE;
2776 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2779 /* execute with input_lock locked */
2780 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2781 unsigned int data_norm,
2782 unsigned long *flags)
2786 if (data_norm == vcs->data_norm)
2789 switch (vcs->input) {
2791 /* only one "norm" supported */
2792 if (data_norm != VINO_DATA_NORM_D1)
2795 case VINO_INPUT_COMPOSITE:
2796 case VINO_INPUT_SVIDEO: {
2799 if ((data_norm != VINO_DATA_NORM_PAL)
2800 && (data_norm != VINO_DATA_NORM_NTSC)
2801 && (data_norm != VINO_DATA_NORM_SECAM))
2804 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
2806 /* Don't hold spinlocks while setting norm
2807 * as it may take a while... */
2809 norm = vino_data_norms[data_norm].std;
2810 err = decoder_call(tuner, s_std, norm);
2812 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2817 vcs->data_norm = data_norm;
2819 vino_set_default_clipping(vcs);
2820 vino_set_default_scaling(vcs);
2821 vino_set_default_framerate(vcs);
2832 /* V4L2 helper functions */
2834 static int vino_find_data_format(__u32 pixelformat)
2838 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2839 if (vino_data_formats[i].pixelformat == pixelformat)
2843 return VINO_DATA_FMT_NONE;
2846 static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
2848 int input = VINO_INPUT_NONE;
2849 unsigned long flags;
2851 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2852 if (vino_drvdata->decoder && vino_drvdata->camera) {
2855 input = VINO_INPUT_COMPOSITE;
2858 input = VINO_INPUT_SVIDEO;
2861 input = VINO_INPUT_D1;
2864 } else if (vino_drvdata->decoder) {
2867 input = VINO_INPUT_COMPOSITE;
2870 input = VINO_INPUT_SVIDEO;
2873 } else if (vino_drvdata->camera) {
2876 input = VINO_INPUT_D1;
2880 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2885 /* execute with input_lock locked */
2886 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2889 // FIXME: detect when no inputs available
2891 if (vino_drvdata->decoder && vino_drvdata->camera) {
2892 switch (vcs->input) {
2893 case VINO_INPUT_COMPOSITE:
2896 case VINO_INPUT_SVIDEO:
2903 } else if (vino_drvdata->decoder) {
2904 switch (vcs->input) {
2905 case VINO_INPUT_COMPOSITE:
2908 case VINO_INPUT_SVIDEO:
2912 } else if (vino_drvdata->camera) {
2913 switch (vcs->input) {
2925 static int vino_querycap(struct file *file, void *__fh,
2926 struct v4l2_capability *cap)
2928 memset(cap, 0, sizeof(struct v4l2_capability));
2930 strcpy(cap->driver, vino_driver_name);
2931 strcpy(cap->card, vino_driver_description);
2932 strcpy(cap->bus_info, vino_bus_name);
2933 cap->version = VINO_VERSION_CODE;
2935 V4L2_CAP_VIDEO_CAPTURE |
2937 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2941 static int vino_enum_input(struct file *file, void *__fh,
2942 struct v4l2_input *i)
2944 struct vino_channel_settings *vcs = video_drvdata(file);
2945 __u32 index = i->index;
2947 dprintk("requested index = %d\n", index);
2949 input = vino_int_enum_input(vcs, index);
2950 if (input == VINO_INPUT_NONE)
2953 memset(i, 0, sizeof(struct v4l2_input));
2956 i->type = V4L2_INPUT_TYPE_CAMERA;
2957 i->std = vino_inputs[input].std;
2958 strcpy(i->name, vino_inputs[input].name);
2960 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2961 decoder_call(video, g_input_status, &i->status);
2965 static int vino_g_input(struct file *file, void *__fh,
2968 struct vino_channel_settings *vcs = video_drvdata(file);
2971 unsigned long flags;
2973 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2975 index = vino_find_input_index(vcs);
2976 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2978 dprintk("input = %d\n", input);
2980 if (input == VINO_INPUT_NONE) {
2989 static int vino_s_input(struct file *file, void *__fh,
2992 struct vino_channel_settings *vcs = video_drvdata(file);
2994 dprintk("requested input = %d\n", i);
2996 input = vino_int_enum_input(vcs, i);
2997 if (input == VINO_INPUT_NONE)
3000 return vino_set_input(vcs, input);
3003 static int vino_querystd(struct file *file, void *__fh,
3006 struct vino_channel_settings *vcs = video_drvdata(file);
3007 unsigned long flags;
3010 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3012 switch (vcs->input) {
3014 *std = vino_inputs[vcs->input].std;
3016 case VINO_INPUT_COMPOSITE:
3017 case VINO_INPUT_SVIDEO: {
3018 decoder_call(video, querystd, std);
3025 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3030 static int vino_g_std(struct file *file, void *__fh,
3033 struct vino_channel_settings *vcs = video_drvdata(file);
3034 unsigned long flags;
3036 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3038 *std = vino_data_norms[vcs->data_norm].std;
3039 dprintk("current standard = %d\n", vcs->data_norm);
3041 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3046 static int vino_s_std(struct file *file, void *__fh,
3049 struct vino_channel_settings *vcs = video_drvdata(file);
3050 unsigned long flags;
3053 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3055 if (!vino_is_input_owner(vcs)) {
3060 /* check if the standard is valid for the current input */
3061 if ((*std) & vino_inputs[vcs->input].std) {
3062 dprintk("standard accepted\n");
3064 /* change the video norm for SAA7191
3065 * and accept NTSC for D1 (do nothing) */
3067 if (vcs->input == VINO_INPUT_D1)
3070 if ((*std) & V4L2_STD_PAL) {
3071 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3073 } else if ((*std) & V4L2_STD_NTSC) {
3074 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3076 } else if ((*std) & V4L2_STD_SECAM) {
3077 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3091 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3096 static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
3097 struct v4l2_fmtdesc *fd)
3099 enum v4l2_buf_type type = fd->type;
3100 int index = fd->index;
3102 dprintk("format index = %d\n", index);
3104 if ((fd->index < 0) ||
3105 (fd->index >= VINO_DATA_FMT_COUNT))
3107 dprintk("format name = %s\n",
3108 vino_data_formats[index].description);
3110 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3113 fd->pixelformat = vino_data_formats[index].pixelformat;
3114 strcpy(fd->description, vino_data_formats[index].description);
3118 static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
3119 struct v4l2_format *f)
3121 struct vino_channel_settings *vcs = video_drvdata(file);
3122 struct vino_channel_settings tempvcs;
3123 unsigned long flags;
3124 struct v4l2_pix_format *pf = &f->fmt.pix;
3126 dprintk("requested: w = %d, h = %d\n",
3127 pf->width, pf->height);
3129 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3130 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3131 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3133 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3134 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3135 tempvcs.data_format = VINO_DATA_FMT_GREY;
3137 vino_data_formats[tempvcs.data_format].
3141 /* data format must be set before clipping/scaling */
3142 vino_set_scaling(&tempvcs, pf->width, pf->height);
3144 dprintk("data format = %s\n",
3145 vino_data_formats[tempvcs.data_format].description);
3147 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3149 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3152 pf->field = V4L2_FIELD_INTERLACED;
3153 pf->bytesperline = tempvcs.line_size;
3154 pf->sizeimage = tempvcs.line_size *
3155 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3158 vino_data_formats[tempvcs.data_format].colorspace;
3164 static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
3165 struct v4l2_format *f)
3167 struct vino_channel_settings *vcs = video_drvdata(file);
3168 unsigned long flags;
3169 struct v4l2_pix_format *pf = &f->fmt.pix;
3171 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3173 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3175 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3178 vino_data_formats[vcs->data_format].pixelformat;
3180 pf->field = V4L2_FIELD_INTERLACED;
3181 pf->bytesperline = vcs->line_size;
3182 pf->sizeimage = vcs->line_size *
3183 (vcs->clipping.bottom - vcs->clipping.top) /
3186 vino_data_formats[vcs->data_format].colorspace;
3190 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3194 static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3195 struct v4l2_format *f)
3197 struct vino_channel_settings *vcs = video_drvdata(file);
3199 unsigned long flags;
3200 struct v4l2_pix_format *pf = &f->fmt.pix;
3202 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3204 data_format = vino_find_data_format(pf->pixelformat);
3206 if (data_format == VINO_DATA_FMT_NONE) {
3207 vcs->data_format = VINO_DATA_FMT_GREY;
3209 vino_data_formats[vcs->data_format].
3212 vcs->data_format = data_format;
3215 /* data format must be set before clipping/scaling */
3216 vino_set_scaling(vcs, pf->width, pf->height);
3218 dprintk("data format = %s\n",
3219 vino_data_formats[vcs->data_format].description);
3221 pf->width = vcs->clipping.right - vcs->clipping.left;
3222 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3224 pf->field = V4L2_FIELD_INTERLACED;
3225 pf->bytesperline = vcs->line_size;
3226 pf->sizeimage = vcs->line_size *
3227 (vcs->clipping.bottom - vcs->clipping.top) /
3230 vino_data_formats[vcs->data_format].colorspace;
3234 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3238 static int vino_cropcap(struct file *file, void *__fh,
3239 struct v4l2_cropcap *ccap)
3241 struct vino_channel_settings *vcs = video_drvdata(file);
3242 const struct vino_data_norm *norm;
3243 unsigned long flags;
3245 switch (ccap->type) {
3246 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3247 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3249 norm = &vino_data_norms[vcs->data_norm];
3251 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3253 ccap->bounds.left = 0;
3254 ccap->bounds.top = 0;
3255 ccap->bounds.width = norm->width;
3256 ccap->bounds.height = norm->height;
3257 memcpy(&ccap->defrect, &ccap->bounds,
3258 sizeof(struct v4l2_rect));
3260 ccap->pixelaspect.numerator = 1;
3261 ccap->pixelaspect.denominator = 1;
3263 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3271 static int vino_g_crop(struct file *file, void *__fh,
3272 struct v4l2_crop *c)
3274 struct vino_channel_settings *vcs = video_drvdata(file);
3275 unsigned long flags;
3278 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3279 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3281 c->c.left = vcs->clipping.left;
3282 c->c.top = vcs->clipping.top;
3283 c->c.width = vcs->clipping.right - vcs->clipping.left;
3284 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3286 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3288 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3296 static int vino_s_crop(struct file *file, void *__fh,
3297 struct v4l2_crop *c)
3299 struct vino_channel_settings *vcs = video_drvdata(file);
3300 unsigned long flags;
3303 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3304 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3306 vino_set_clipping(vcs, c->c.left, c->c.top,
3307 c->c.width, c->c.height);
3309 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3311 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3319 static int vino_g_parm(struct file *file, void *__fh,
3320 struct v4l2_streamparm *sp)
3322 struct vino_channel_settings *vcs = video_drvdata(file);
3323 unsigned long flags;
3326 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3327 struct v4l2_captureparm *cp = &sp->parm.capture;
3328 memset(cp, 0, sizeof(struct v4l2_captureparm));
3330 cp->capability = V4L2_CAP_TIMEPERFRAME;
3331 cp->timeperframe.numerator = 1;
3333 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3335 cp->timeperframe.denominator = vcs->fps;
3337 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3339 // TODO: cp->readbuffers = xxx;
3342 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3350 static int vino_s_parm(struct file *file, void *__fh,
3351 struct v4l2_streamparm *sp)
3353 struct vino_channel_settings *vcs = video_drvdata(file);
3354 unsigned long flags;
3357 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3358 struct v4l2_captureparm *cp = &sp->parm.capture;
3360 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3362 if ((cp->timeperframe.numerator == 0) ||
3363 (cp->timeperframe.denominator == 0)) {
3364 /* reset framerate */
3365 vino_set_default_framerate(vcs);
3367 vino_set_framerate(vcs, cp->timeperframe.denominator /
3368 cp->timeperframe.numerator);
3371 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3373 // TODO: set buffers according to cp->readbuffers
3376 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3384 static int vino_reqbufs(struct file *file, void *__fh,
3385 struct v4l2_requestbuffers *rb)
3387 struct vino_channel_settings *vcs = video_drvdata(file);
3392 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3393 // TODO: check queue type
3394 if (rb->memory != V4L2_MEMORY_MMAP) {
3395 dprintk("type not mmap\n");
3399 dprintk("count = %d\n", rb->count);
3400 if (rb->count > 0) {
3401 if (vino_is_capturing(vcs)) {
3402 dprintk("busy, capturing\n");
3406 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3407 dprintk("busy, buffers still mapped\n");
3411 vino_queue_free(&vcs->fb_queue);
3412 vino_queue_init(&vcs->fb_queue, &rb->count);
3416 vino_capture_stop(vcs);
3417 vino_queue_free(&vcs->fb_queue);
3421 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3429 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3430 struct vino_framebuffer *fb,
3431 struct v4l2_buffer *b)
3433 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3435 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3436 b->flags |= V4L2_BUF_FLAG_DONE;
3437 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3439 b->flags &= ~V4L2_BUF_FLAG_DONE;
3440 b->flags |= V4L2_BUF_FLAG_QUEUED;
3442 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3443 V4L2_BUF_FLAG_QUEUED);
3446 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3448 if (fb->map_count > 0)
3449 b->flags |= V4L2_BUF_FLAG_MAPPED;
3452 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3453 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3454 b->m.offset = fb->offset;
3455 b->bytesused = fb->data_size;
3456 b->length = fb->size;
3457 b->field = V4L2_FIELD_INTERLACED;
3458 b->sequence = fb->frame_counter;
3459 memcpy(&b->timestamp, &fb->timestamp,
3460 sizeof(struct timeval));
3463 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3464 fb->id, fb->size, fb->data_size, fb->offset);
3467 static int vino_querybuf(struct file *file, void *__fh,
3468 struct v4l2_buffer *b)
3470 struct vino_channel_settings *vcs = video_drvdata(file);
3475 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3476 struct vino_framebuffer *fb;
3478 // TODO: check queue type
3479 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3480 dprintk("invalid index = %d\n",
3485 fb = vino_queue_get_buffer(&vcs->fb_queue,
3488 dprintk("vino_queue_get_buffer() failed");
3492 vino_v4l2_get_buffer_status(vcs, fb, b);
3495 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3503 static int vino_qbuf(struct file *file, void *__fh,
3504 struct v4l2_buffer *b)
3506 struct vino_channel_settings *vcs = video_drvdata(file);
3511 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3512 struct vino_framebuffer *fb;
3515 // TODO: check queue type
3516 if (b->memory != V4L2_MEMORY_MMAP) {
3517 dprintk("type not mmap\n");
3521 fb = vino_capture_enqueue(vcs, b->index);
3525 vino_v4l2_get_buffer_status(vcs, fb, b);
3527 if (vcs->streaming) {
3528 ret = vino_capture_next(vcs, 1);
3534 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3542 static int vino_dqbuf(struct file *file, void *__fh,
3543 struct v4l2_buffer *b)
3545 struct vino_channel_settings *vcs = video_drvdata(file);
3546 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3551 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3552 struct vino_framebuffer *fb;
3553 unsigned int incoming, outgoing;
3556 // TODO: check queue type
3558 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3560 dprintk("vino_queue_get_incoming() failed\n");
3563 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3565 dprintk("vino_queue_get_outgoing() failed\n");
3569 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3571 if (outgoing == 0) {
3572 if (incoming == 0) {
3573 dprintk("no incoming or outgoing buffers\n");
3577 dprintk("non-blocking I/O was selected and "
3578 "there are no buffers to dequeue\n");
3582 err = vino_wait_for_frame(vcs);
3584 err = vino_wait_for_frame(vcs);
3587 * no frames captured because
3588 * of frame skipping */
3589 // vino_capture_failed(vcs);
3595 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3597 dprintk("vino_queue_remove() failed\n");
3601 err = vino_check_buffer(vcs, fb);
3603 vino_v4l2_get_buffer_status(vcs, fb, b);
3610 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3618 static int vino_streamon(struct file *file, void *__fh,
3619 enum v4l2_buf_type i)
3621 struct vino_channel_settings *vcs = video_drvdata(file);
3622 unsigned int incoming;
3630 // TODO: check queue type
3632 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3633 dprintk("no buffers allocated\n");
3637 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3639 dprintk("vino_queue_get_incoming() failed\n");
3646 ret = vino_capture_next(vcs, 1);
3650 dprintk("couldn't start capture\n");
3658 static int vino_streamoff(struct file *file, void *__fh,
3659 enum v4l2_buf_type i)
3661 struct vino_channel_settings *vcs = video_drvdata(file);
3665 if (!vcs->streaming)
3669 vino_capture_stop(vcs);
3674 static int vino_queryctrl(struct file *file, void *__fh,
3675 struct v4l2_queryctrl *queryctrl)
3677 struct vino_channel_settings *vcs = video_drvdata(file);
3678 unsigned long flags;
3682 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3684 switch (vcs->input) {
3686 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3687 if (vino_indycam_v4l2_controls[i].id ==
3690 &vino_indycam_v4l2_controls[i],
3691 sizeof(struct v4l2_queryctrl));
3692 queryctrl->reserved[0] = 0;
3699 case VINO_INPUT_COMPOSITE:
3700 case VINO_INPUT_SVIDEO:
3701 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3702 if (vino_saa7191_v4l2_controls[i].id ==
3705 &vino_saa7191_v4l2_controls[i],
3706 sizeof(struct v4l2_queryctrl));
3707 queryctrl->reserved[0] = 0;
3719 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3724 static int vino_g_ctrl(struct file *file, void *__fh,
3725 struct v4l2_control *control)
3727 struct vino_channel_settings *vcs = video_drvdata(file);
3728 unsigned long flags;
3732 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3734 switch (vcs->input) {
3735 case VINO_INPUT_D1: {
3737 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3738 if (vino_indycam_v4l2_controls[i].id == control->id) {
3747 err = camera_call(core, g_ctrl, control);
3752 case VINO_INPUT_COMPOSITE:
3753 case VINO_INPUT_SVIDEO: {
3755 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3756 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3765 err = decoder_call(core, g_ctrl, control);
3775 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3780 static int vino_s_ctrl(struct file *file, void *__fh,
3781 struct v4l2_control *control)
3783 struct vino_channel_settings *vcs = video_drvdata(file);
3784 unsigned long flags;
3788 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3790 if (!vino_is_input_owner(vcs)) {
3795 switch (vcs->input) {
3796 case VINO_INPUT_D1: {
3798 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3799 if (vino_indycam_v4l2_controls[i].id == control->id) {
3806 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3807 control->value > vino_indycam_v4l2_controls[i].maximum) {
3811 err = camera_call(core, s_ctrl, control);
3816 case VINO_INPUT_COMPOSITE:
3817 case VINO_INPUT_SVIDEO: {
3819 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3820 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3827 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3828 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3833 err = decoder_call(core, s_ctrl, control);
3843 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3848 /* File operations */
3850 static int vino_open(struct file *file)
3852 struct vino_channel_settings *vcs = video_drvdata(file);
3854 dprintk("open(): channel = %c\n",
3855 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3857 mutex_lock(&vcs->mutex);
3860 dprintk("open(): driver busy\n");
3865 ret = vino_acquire_input(vcs);
3867 dprintk("open(): vino_acquire_input() failed\n");
3874 mutex_unlock(&vcs->mutex);
3876 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3881 static int vino_close(struct file *file)
3883 struct vino_channel_settings *vcs = video_drvdata(file);
3884 dprintk("close():\n");
3886 mutex_lock(&vcs->mutex);
3891 vino_release_input(vcs);
3893 /* stop DMA and free buffers */
3894 vino_capture_stop(vcs);
3895 vino_queue_free(&vcs->fb_queue);
3898 mutex_unlock(&vcs->mutex);
3903 static void vino_vm_open(struct vm_area_struct *vma)
3905 struct vino_framebuffer *fb = vma->vm_private_data;
3908 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3911 static void vino_vm_close(struct vm_area_struct *vma)
3913 struct vino_framebuffer *fb = vma->vm_private_data;
3916 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3919 static struct vm_operations_struct vino_vm_ops = {
3920 .open = vino_vm_open,
3921 .close = vino_vm_close,
3924 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3926 struct vino_channel_settings *vcs = video_drvdata(file);
3928 unsigned long start = vma->vm_start;
3929 unsigned long size = vma->vm_end - vma->vm_start;
3930 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3932 struct vino_framebuffer *fb = NULL;
3933 unsigned int i, length;
3936 dprintk("mmap():\n");
3938 // TODO: reject mmap if already mapped
3940 if (mutex_lock_interruptible(&vcs->mutex))
3948 // TODO: check queue type
3950 if (!(vma->vm_flags & VM_WRITE)) {
3951 dprintk("mmap(): app bug: PROT_WRITE please\n");
3955 if (!(vma->vm_flags & VM_SHARED)) {
3956 dprintk("mmap(): app bug: MAP_SHARED please\n");
3961 /* find the correct buffer using offset */
3962 length = vino_queue_get_length(&vcs->fb_queue);
3964 dprintk("mmap(): queue not initialized\n");
3969 for (i = 0; i < length; i++) {
3970 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3972 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3977 if (fb->offset == offset)
3981 dprintk("mmap(): invalid offset = %lu\n", offset);
3986 dprintk("mmap(): buffer = %d\n", i);
3988 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3989 dprintk("mmap(): failed: size = %lu > %lu\n",
3990 size, fb->desc_table.page_count * PAGE_SIZE);
3995 for (i = 0; i < fb->desc_table.page_count; i++) {
3997 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
4000 if (size < PAGE_SIZE)
4003 // protection was: PAGE_READONLY
4004 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
4005 vma->vm_page_prot)) {
4006 dprintk("mmap(): remap_pfn_range() failed\n");
4017 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
4018 vma->vm_flags &= ~VM_IO;
4019 vma->vm_private_data = fb;
4020 vma->vm_file = file;
4021 vma->vm_ops = &vino_vm_ops;
4024 mutex_unlock(&vcs->mutex);
4029 static unsigned int vino_poll(struct file *file, poll_table *pt)
4031 struct vino_channel_settings *vcs = video_drvdata(file);
4032 unsigned int outgoing;
4033 unsigned int ret = 0;
4036 // TODO: this has to be corrected for different read modes
4038 dprintk("poll():\n");
4040 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4041 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4048 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
4050 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4051 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4057 dprintk("poll(): data %savailable\n",
4058 (outgoing > 0) ? "" : "not ");
4061 ret = POLLIN | POLLRDNORM;
4067 static long vino_ioctl(struct file *file,
4068 unsigned int cmd, unsigned long arg)
4070 struct vino_channel_settings *vcs = video_drvdata(file);
4073 if (mutex_lock_interruptible(&vcs->mutex))
4076 ret = video_ioctl2(file, cmd, arg);
4078 mutex_unlock(&vcs->mutex);
4083 /* Initialization and cleanup */
4086 static int vino_init_stage;
4088 const struct v4l2_ioctl_ops vino_ioctl_ops = {
4089 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4090 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4091 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4092 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4093 .vidioc_querycap = vino_querycap,
4094 .vidioc_enum_input = vino_enum_input,
4095 .vidioc_g_input = vino_g_input,
4096 .vidioc_s_input = vino_s_input,
4097 .vidioc_g_std = vino_g_std,
4098 .vidioc_s_std = vino_s_std,
4099 .vidioc_querystd = vino_querystd,
4100 .vidioc_cropcap = vino_cropcap,
4101 .vidioc_s_crop = vino_s_crop,
4102 .vidioc_g_crop = vino_g_crop,
4103 .vidioc_s_parm = vino_s_parm,
4104 .vidioc_g_parm = vino_g_parm,
4105 .vidioc_reqbufs = vino_reqbufs,
4106 .vidioc_querybuf = vino_querybuf,
4107 .vidioc_qbuf = vino_qbuf,
4108 .vidioc_dqbuf = vino_dqbuf,
4109 .vidioc_streamon = vino_streamon,
4110 .vidioc_streamoff = vino_streamoff,
4111 .vidioc_queryctrl = vino_queryctrl,
4112 .vidioc_g_ctrl = vino_g_ctrl,
4113 .vidioc_s_ctrl = vino_s_ctrl,
4116 static const struct v4l2_file_operations vino_fops = {
4117 .owner = THIS_MODULE,
4119 .release = vino_close,
4120 .unlocked_ioctl = vino_ioctl,
4125 static struct video_device vdev_template = {
4128 .ioctl_ops = &vino_ioctl_ops,
4129 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
4133 static void vino_module_cleanup(int stage)
4137 video_unregister_device(vino_drvdata->b.vdev);
4138 vino_drvdata->b.vdev = NULL;
4140 video_unregister_device(vino_drvdata->a.vdev);
4141 vino_drvdata->a.vdev = NULL;
4143 i2c_del_adapter(&vino_i2c_adapter);
4145 free_irq(SGI_VINO_IRQ, NULL);
4147 if (vino_drvdata->b.vdev) {
4148 video_device_release(vino_drvdata->b.vdev);
4149 vino_drvdata->b.vdev = NULL;
4152 if (vino_drvdata->a.vdev) {
4153 video_device_release(vino_drvdata->a.vdev);
4154 vino_drvdata->a.vdev = NULL;
4157 /* all entries in dma_cpu dummy table have the same address */
4158 dma_unmap_single(NULL,
4159 vino_drvdata->dummy_desc_table.dma_cpu[0],
4160 PAGE_SIZE, DMA_FROM_DEVICE);
4161 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4162 * sizeof(dma_addr_t),
4163 (void *)vino_drvdata->
4164 dummy_desc_table.dma_cpu,
4165 vino_drvdata->dummy_desc_table.dma);
4167 free_page(vino_drvdata->dummy_page);
4169 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
4171 kfree(vino_drvdata);
4177 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4182 static int vino_probe(void)
4184 unsigned long rev_id;
4186 if (ip22_is_fullhouse()) {
4187 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4191 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4192 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4196 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4198 printk(KERN_ERR "VINO: ioremap() failed\n");
4203 if (get_dbe(rev_id, &(vino->rev_id))) {
4204 printk(KERN_ERR "Failed to read VINO revision register\n");
4205 vino_module_cleanup(vino_init_stage);
4209 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4210 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4212 vino_module_cleanup(vino_init_stage);
4216 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
4221 static int vino_init(void)
4223 dma_addr_t dma_dummy_address;
4227 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
4228 if (!vino_drvdata) {
4229 vino_module_cleanup(vino_init_stage);
4233 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4234 sizeof(vino_drvdata->v4l2_dev.name));
4235 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4240 /* create a dummy dma descriptor */
4241 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4242 if (!vino_drvdata->dummy_page) {
4243 vino_module_cleanup(vino_init_stage);
4248 // TODO: use page_count in dummy_desc_table
4250 vino_drvdata->dummy_desc_table.dma_cpu =
4251 dma_alloc_coherent(NULL,
4252 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4253 &vino_drvdata->dummy_desc_table.dma,
4254 GFP_KERNEL | GFP_DMA);
4255 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4256 vino_module_cleanup(vino_init_stage);
4261 dma_dummy_address = dma_map_single(NULL,
4262 (void *)vino_drvdata->dummy_page,
4263 PAGE_SIZE, DMA_FROM_DEVICE);
4264 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4265 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4268 /* initialize VINO */
4271 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4272 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4273 udelay(VINO_DESC_FETCH_DELAY);
4275 vino->intr_status = 0;
4277 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4278 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4283 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4284 unsigned int channel, const char *name)
4286 vcs->channel = channel;
4287 vcs->input = VINO_INPUT_NONE;
4290 vcs->data_format = VINO_DATA_FMT_GREY;
4291 vcs->data_norm = VINO_DATA_NORM_NTSC;
4292 vcs->decimation = 1;
4293 vino_set_default_clipping(vcs);
4294 vino_set_default_framerate(vcs);
4298 mutex_init(&vcs->mutex);
4299 spin_lock_init(&vcs->capture_lock);
4301 mutex_init(&vcs->fb_queue.queue_mutex);
4302 spin_lock_init(&vcs->fb_queue.queue_lock);
4303 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4305 vcs->vdev = video_device_alloc();
4307 vino_module_cleanup(vino_init_stage);
4312 memcpy(vcs->vdev, &vdev_template,
4313 sizeof(struct video_device));
4314 strcpy(vcs->vdev->name, name);
4315 vcs->vdev->release = video_device_release;
4316 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
4318 video_set_drvdata(vcs->vdev, vcs);
4323 static int __init vino_module_init(void)
4325 unsigned short addr[] = { 0, I2C_CLIENT_END };
4328 printk(KERN_INFO "SGI VINO driver version %s\n",
4329 VINO_MODULE_VERSION);
4339 /* initialize data structures */
4341 spin_lock_init(&vino_drvdata->vino_lock);
4342 spin_lock_init(&vino_drvdata->input_lock);
4344 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4349 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4354 /* initialize hardware and register V4L devices */
4356 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4357 vino_driver_description, NULL);
4359 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4361 vino_module_cleanup(vino_init_stage);
4366 ret = i2c_add_adapter(&vino_i2c_adapter);
4368 printk(KERN_ERR "VINO I2C bus registration failed\n");
4369 vino_module_cleanup(vino_init_stage);
4372 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
4375 ret = video_register_device(vino_drvdata->a.vdev,
4376 VFL_TYPE_GRABBER, -1);
4378 printk(KERN_ERR "VINO channel A Video4Linux-device "
4379 "registration failed\n");
4380 vino_module_cleanup(vino_init_stage);
4385 ret = video_register_device(vino_drvdata->b.vdev,
4386 VFL_TYPE_GRABBER, -1);
4388 printk(KERN_ERR "VINO channel B Video4Linux-device "
4389 "registration failed\n");
4390 vino_module_cleanup(vino_init_stage);
4396 vino_drvdata->decoder = v4l2_i2c_new_probed_subdev(&vino_i2c_adapter,
4397 "saa7191", "saa7191", addr);
4399 vino_drvdata->camera = v4l2_i2c_new_probed_subdev(&vino_i2c_adapter,
4400 "indycam", "indycam", addr);
4402 dprintk("init complete!\n");
4407 static void __exit vino_module_exit(void)
4409 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4410 vino_module_cleanup(vino_init_stage);
4411 dprintk("cleanup complete, exit!\n");
4414 module_init(vino_module_init);
4415 module_exit(vino_module_exit);