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>
36 #include <linux/i2c-algo-sgi.h>
38 #include <linux/videodev2.h>
39 #include <media/v4l2-common.h>
40 #include <media/v4l2-ioctl.h>
41 #include <linux/mutex.h>
43 #include <asm/paccess.h>
45 #include <asm/sgi/ip22.h>
46 #include <asm/sgi/mc.h>
52 /* Uncomment the following line to get lots and lots of (mostly useless)
54 * Note that the debug output also slows down the driver significantly */
56 // #define VINO_DEBUG_INT
58 #define VINO_MODULE_VERSION "0.0.5"
59 #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
61 MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
62 MODULE_VERSION(VINO_MODULE_VERSION);
63 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
64 MODULE_LICENSE("GPL");
67 #define dprintk(x...) printk("VINO: " x);
72 #define VINO_NO_CHANNEL 0
73 #define VINO_CHANNEL_A 1
74 #define VINO_CHANNEL_B 2
76 #define VINO_PAL_WIDTH 768
77 #define VINO_PAL_HEIGHT 576
78 #define VINO_NTSC_WIDTH 640
79 #define VINO_NTSC_HEIGHT 480
81 #define VINO_MIN_WIDTH 32
82 #define VINO_MIN_HEIGHT 32
84 #define VINO_CLIPPING_START_ODD_D1 1
85 #define VINO_CLIPPING_START_ODD_PAL 15
86 #define VINO_CLIPPING_START_ODD_NTSC 12
88 #define VINO_CLIPPING_START_EVEN_D1 2
89 #define VINO_CLIPPING_START_EVEN_PAL 15
90 #define VINO_CLIPPING_START_EVEN_NTSC 12
92 #define VINO_INPUT_CHANNEL_COUNT 3
94 /* the number is the index for vino_inputs */
95 #define VINO_INPUT_NONE -1
96 #define VINO_INPUT_COMPOSITE 0
97 #define VINO_INPUT_SVIDEO 1
98 #define VINO_INPUT_D1 2
100 #define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
102 #define VINO_FIFO_THRESHOLD_DEFAULT 16
104 #define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
105 * VINO_PAL_HEIGHT * 4 \
106 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
108 #define VINO_FRAMEBUFFER_COUNT_MAX 8
110 #define VINO_FRAMEBUFFER_UNUSED 0
111 #define VINO_FRAMEBUFFER_IN_USE 1
112 #define VINO_FRAMEBUFFER_READY 2
114 #define VINO_QUEUE_ERROR -1
115 #define VINO_QUEUE_MAGIC 0x20050125
117 #define VINO_MEMORY_NONE 0
118 #define VINO_MEMORY_MMAP 1
119 #define VINO_MEMORY_USERPTR 2
121 #define VINO_DUMMY_DESC_COUNT 4
122 #define VINO_DESC_FETCH_DELAY 5 /* microseconds */
124 #define VINO_MAX_FRAME_SKIP_COUNT 128
126 /* the number is the index for vino_data_formats */
127 #define VINO_DATA_FMT_NONE -1
128 #define VINO_DATA_FMT_GREY 0
129 #define VINO_DATA_FMT_RGB332 1
130 #define VINO_DATA_FMT_RGB32 2
131 #define VINO_DATA_FMT_YUV 3
133 #define VINO_DATA_FMT_COUNT 4
135 /* the number is the index for vino_data_norms */
136 #define VINO_DATA_NORM_NONE -1
137 #define VINO_DATA_NORM_NTSC 0
138 #define VINO_DATA_NORM_PAL 1
139 #define VINO_DATA_NORM_SECAM 2
140 #define VINO_DATA_NORM_D1 3
142 #define VINO_DATA_NORM_COUNT 4
144 /* Internal data structure definitions */
151 struct vino_clipping {
152 unsigned int left, right, top, bottom;
155 struct vino_data_format {
156 /* the description */
158 /* bytes per pixel */
160 /* V4L2 fourcc code */
162 /* V4L2 colorspace (duh!) */
163 enum v4l2_colorspace colorspace;
166 struct vino_data_norm {
168 unsigned int width, height;
169 struct vino_clipping odd;
170 struct vino_clipping even;
173 unsigned int fps_min, fps_max;
177 struct vino_descriptor_table {
178 /* the number of PAGE_SIZE sized pages in the buffer */
179 unsigned int page_count;
180 /* virtual (kmalloc'd) pointers to the actual data
181 * (in PAGE_SIZE chunks, used with mmap streaming) */
182 unsigned long *virtual;
184 /* cpu address for the VINO descriptor table
185 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
186 unsigned long *dma_cpu;
187 /* dma address for the VINO descriptor table
188 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
192 struct vino_framebuffer {
193 /* identifier nubmer */
195 /* the length of the whole buffer */
197 /* the length of actual data in buffer */
198 unsigned int data_size;
199 /* the data format */
200 unsigned int data_format;
201 /* the state of buffer data */
203 /* is the buffer mapped in user space? */
204 unsigned int map_count;
205 /* memory offset for mmap() */
208 unsigned int frame_counter;
209 /* timestamp (written when image capture finishes) */
210 struct timeval timestamp;
212 struct vino_descriptor_table desc_table;
214 spinlock_t state_lock;
217 struct vino_framebuffer_fifo {
224 unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
227 struct vino_framebuffer_queue {
230 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
234 /* data field of in and out contain index numbers for buffer */
235 struct vino_framebuffer_fifo in;
236 struct vino_framebuffer_fifo out;
238 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
240 spinlock_t queue_lock;
241 struct mutex queue_mutex;
242 wait_queue_head_t frame_wait_queue;
245 struct vino_interrupt_data {
246 struct timeval timestamp;
247 unsigned int frame_counter;
248 unsigned int skip_count;
252 struct vino_channel_settings {
253 unsigned int channel;
256 unsigned int data_format;
257 unsigned int data_norm;
258 struct vino_clipping clipping;
259 unsigned int decimation;
260 unsigned int line_size;
263 unsigned int framert_reg;
265 unsigned int fifo_threshold;
267 struct vino_framebuffer_queue fb_queue;
269 /* number of the current field */
272 /* read in progress */
274 /* streaming is active */
276 /* the driver is currently processing the queue */
280 spinlock_t capture_lock;
284 struct vino_interrupt_data int_data;
287 struct video_device *vdev;
291 /* the channel which owns this client:
292 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
294 struct i2c_client *driver;
297 struct vino_settings {
298 struct vino_channel_settings a;
299 struct vino_channel_settings b;
301 struct vino_client decoder;
302 struct vino_client camera;
304 /* a lock for vino register access */
305 spinlock_t vino_lock;
306 /* a lock for channel input changes */
307 spinlock_t input_lock;
309 unsigned long dummy_page;
310 struct vino_descriptor_table dummy_desc_table;
313 /* Module parameters */
316 * Using vino_pixel_conversion the ABGR32-format pixels supplied
317 * by the VINO chip can be converted to more common formats
318 * like RGBA32 (or probably RGB24 in the future). This way we
319 * can give out data that can be specified correctly with
320 * the V4L2-definitions.
322 * The pixel format is specified as RGBA32 when no conversion
325 * Note that this only affects the 32-bit bit depth.
327 * Use non-zero value to enable conversion.
329 static int vino_pixel_conversion;
331 module_param_named(pixelconv, vino_pixel_conversion, int, 0);
333 MODULE_PARM_DESC(pixelconv,
334 "enable pixel conversion (non-zero value enables)");
336 /* Internal data structures */
338 static struct sgi_vino *vino;
340 static struct vino_settings *vino_drvdata;
342 static const char *vino_driver_name = "vino";
343 static const char *vino_driver_description = "SGI VINO";
344 static const char *vino_bus_name = "GIO64 bus";
345 static const char *vino_vdev_name_a = "SGI VINO Channel A";
346 static const char *vino_vdev_name_b = "SGI VINO Channel B";
348 static void vino_capture_tasklet(unsigned long channel);
350 DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
351 DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
353 static const struct vino_input vino_inputs[] = {
356 .std = V4L2_STD_NTSC | V4L2_STD_PAL
360 .std = V4L2_STD_NTSC | V4L2_STD_PAL
363 .name = "D1/IndyCam",
364 .std = V4L2_STD_NTSC,
368 static const struct vino_data_format vino_data_formats[] = {
370 .description = "8-bit greyscale",
372 .pixelformat = V4L2_PIX_FMT_GREY,
373 .colorspace = V4L2_COLORSPACE_SMPTE170M,
375 .description = "8-bit dithered RGB 3-3-2",
377 .pixelformat = V4L2_PIX_FMT_RGB332,
378 .colorspace = V4L2_COLORSPACE_SRGB,
380 .description = "32-bit RGB",
382 .pixelformat = V4L2_PIX_FMT_RGB32,
383 .colorspace = V4L2_COLORSPACE_SRGB,
385 .description = "YUV 4:2:2",
387 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
388 .colorspace = V4L2_COLORSPACE_SMPTE170M,
392 static const struct vino_data_norm vino_data_norms[] = {
394 .description = "NTSC",
395 .std = V4L2_STD_NTSC,
399 .width = VINO_NTSC_WIDTH,
400 .height = VINO_NTSC_HEIGHT,
402 .top = VINO_CLIPPING_START_ODD_NTSC,
404 .bottom = VINO_CLIPPING_START_ODD_NTSC
405 + VINO_NTSC_HEIGHT / 2 - 1,
406 .right = VINO_NTSC_WIDTH,
409 .top = VINO_CLIPPING_START_EVEN_NTSC,
411 .bottom = VINO_CLIPPING_START_EVEN_NTSC
412 + VINO_NTSC_HEIGHT / 2 - 1,
413 .right = VINO_NTSC_WIDTH,
416 .description = "PAL",
421 .width = VINO_PAL_WIDTH,
422 .height = VINO_PAL_HEIGHT,
424 .top = VINO_CLIPPING_START_ODD_PAL,
426 .bottom = VINO_CLIPPING_START_ODD_PAL
427 + VINO_PAL_HEIGHT / 2 - 1,
428 .right = VINO_PAL_WIDTH,
431 .top = VINO_CLIPPING_START_EVEN_PAL,
433 .bottom = VINO_CLIPPING_START_EVEN_PAL
434 + VINO_PAL_HEIGHT / 2 - 1,
435 .right = VINO_PAL_WIDTH,
438 .description = "SECAM",
439 .std = V4L2_STD_SECAM,
443 .width = VINO_PAL_WIDTH,
444 .height = VINO_PAL_HEIGHT,
446 .top = VINO_CLIPPING_START_ODD_PAL,
448 .bottom = VINO_CLIPPING_START_ODD_PAL
449 + VINO_PAL_HEIGHT / 2 - 1,
450 .right = VINO_PAL_WIDTH,
453 .top = VINO_CLIPPING_START_EVEN_PAL,
455 .bottom = VINO_CLIPPING_START_EVEN_PAL
456 + VINO_PAL_HEIGHT / 2 - 1,
457 .right = VINO_PAL_WIDTH,
460 .description = "NTSC/D1",
461 .std = V4L2_STD_NTSC,
465 .width = VINO_NTSC_WIDTH,
466 .height = VINO_NTSC_HEIGHT,
468 .top = VINO_CLIPPING_START_ODD_D1,
470 .bottom = VINO_CLIPPING_START_ODD_D1
471 + VINO_NTSC_HEIGHT / 2 - 1,
472 .right = VINO_NTSC_WIDTH,
475 .top = VINO_CLIPPING_START_EVEN_D1,
477 .bottom = VINO_CLIPPING_START_EVEN_D1
478 + VINO_NTSC_HEIGHT / 2 - 1,
479 .right = VINO_NTSC_WIDTH,
484 #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
486 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
488 .id = V4L2_CID_AUTOGAIN,
489 .type = V4L2_CTRL_TYPE_BOOLEAN,
490 .name = "Automatic Gain Control",
494 .default_value = INDYCAM_AGC_DEFAULT,
496 .id = V4L2_CID_AUTO_WHITE_BALANCE,
497 .type = V4L2_CTRL_TYPE_BOOLEAN,
498 .name = "Automatic White Balance",
502 .default_value = INDYCAM_AWB_DEFAULT,
505 .type = V4L2_CTRL_TYPE_INTEGER,
507 .minimum = INDYCAM_GAIN_MIN,
508 .maximum = INDYCAM_GAIN_MAX,
510 .default_value = INDYCAM_GAIN_DEFAULT,
512 .id = INDYCAM_CONTROL_RED_SATURATION,
513 .type = V4L2_CTRL_TYPE_INTEGER,
514 .name = "Red Saturation",
515 .minimum = INDYCAM_RED_SATURATION_MIN,
516 .maximum = INDYCAM_RED_SATURATION_MAX,
518 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
520 .id = INDYCAM_CONTROL_BLUE_SATURATION,
521 .type = V4L2_CTRL_TYPE_INTEGER,
522 .name = "Blue Saturation",
523 .minimum = INDYCAM_BLUE_SATURATION_MIN,
524 .maximum = INDYCAM_BLUE_SATURATION_MAX,
526 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
528 .id = V4L2_CID_RED_BALANCE,
529 .type = V4L2_CTRL_TYPE_INTEGER,
530 .name = "Red Balance",
531 .minimum = INDYCAM_RED_BALANCE_MIN,
532 .maximum = INDYCAM_RED_BALANCE_MAX,
534 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
536 .id = V4L2_CID_BLUE_BALANCE,
537 .type = V4L2_CTRL_TYPE_INTEGER,
538 .name = "Blue Balance",
539 .minimum = INDYCAM_BLUE_BALANCE_MIN,
540 .maximum = INDYCAM_BLUE_BALANCE_MAX,
542 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
544 .id = V4L2_CID_EXPOSURE,
545 .type = V4L2_CTRL_TYPE_INTEGER,
546 .name = "Shutter Control",
547 .minimum = INDYCAM_SHUTTER_MIN,
548 .maximum = INDYCAM_SHUTTER_MAX,
550 .default_value = INDYCAM_SHUTTER_DEFAULT,
552 .id = V4L2_CID_GAMMA,
553 .type = V4L2_CTRL_TYPE_INTEGER,
555 .minimum = INDYCAM_GAMMA_MIN,
556 .maximum = INDYCAM_GAMMA_MAX,
558 .default_value = INDYCAM_GAMMA_DEFAULT,
562 #define VINO_SAA7191_V4L2_CONTROL_COUNT 9
564 struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
567 .type = V4L2_CTRL_TYPE_INTEGER,
569 .minimum = SAA7191_HUE_MIN,
570 .maximum = SAA7191_HUE_MAX,
572 .default_value = SAA7191_HUE_DEFAULT,
574 .id = SAA7191_CONTROL_BANDPASS,
575 .type = V4L2_CTRL_TYPE_INTEGER,
576 .name = "Luminance Bandpass",
577 .minimum = SAA7191_BANDPASS_MIN,
578 .maximum = SAA7191_BANDPASS_MAX,
580 .default_value = SAA7191_BANDPASS_DEFAULT,
582 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
583 .type = V4L2_CTRL_TYPE_INTEGER,
584 .name = "Luminance Bandpass Weight",
585 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
586 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
588 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
590 .id = SAA7191_CONTROL_CORING,
591 .type = V4L2_CTRL_TYPE_INTEGER,
592 .name = "HF Luminance Coring",
593 .minimum = SAA7191_CORING_MIN,
594 .maximum = SAA7191_CORING_MAX,
596 .default_value = SAA7191_CORING_DEFAULT,
598 .id = SAA7191_CONTROL_FORCE_COLOUR,
599 .type = V4L2_CTRL_TYPE_BOOLEAN,
600 .name = "Force Colour",
601 .minimum = SAA7191_FORCE_COLOUR_MIN,
602 .maximum = SAA7191_FORCE_COLOUR_MAX,
604 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
606 .id = SAA7191_CONTROL_CHROMA_GAIN,
607 .type = V4L2_CTRL_TYPE_INTEGER,
608 .name = "Chrominance Gain Control",
609 .minimum = SAA7191_CHROMA_GAIN_MIN,
610 .maximum = SAA7191_CHROMA_GAIN_MAX,
612 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
614 .id = SAA7191_CONTROL_VTRC,
615 .type = V4L2_CTRL_TYPE_BOOLEAN,
616 .name = "VTR Time Constant",
617 .minimum = SAA7191_VTRC_MIN,
618 .maximum = SAA7191_VTRC_MAX,
620 .default_value = SAA7191_VTRC_DEFAULT,
622 .id = SAA7191_CONTROL_LUMA_DELAY,
623 .type = V4L2_CTRL_TYPE_INTEGER,
624 .name = "Luminance Delay Compensation",
625 .minimum = SAA7191_LUMA_DELAY_MIN,
626 .maximum = SAA7191_LUMA_DELAY_MAX,
628 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
630 .id = SAA7191_CONTROL_VNR,
631 .type = V4L2_CTRL_TYPE_INTEGER,
632 .name = "Vertical Noise Reduction",
633 .minimum = SAA7191_VNR_MIN,
634 .maximum = SAA7191_VNR_MAX,
636 .default_value = SAA7191_VNR_DEFAULT,
640 /* VINO I2C bus functions */
642 unsigned i2c_vino_getctrl(void *data)
644 return vino->i2c_control;
647 void i2c_vino_setctrl(void *data, unsigned val)
649 vino->i2c_control = val;
652 unsigned i2c_vino_rdata(void *data)
654 return vino->i2c_data;
657 void i2c_vino_wdata(void *data, unsigned val)
659 vino->i2c_data = val;
662 static struct i2c_algo_sgi_data i2c_sgi_vino_data =
664 .getctrl = &i2c_vino_getctrl,
665 .setctrl = &i2c_vino_setctrl,
666 .rdata = &i2c_vino_rdata,
667 .wdata = &i2c_vino_wdata,
673 * There are two possible clients on VINO I2C bus, so we limit usage only
676 static int i2c_vino_client_reg(struct i2c_client *client)
681 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
682 switch (client->driver->id) {
683 case I2C_DRIVERID_SAA7191:
684 if (vino_drvdata->decoder.driver)
687 vino_drvdata->decoder.driver = client;
689 case I2C_DRIVERID_INDYCAM:
690 if (vino_drvdata->camera.driver)
693 vino_drvdata->camera.driver = client;
698 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
703 static int i2c_vino_client_unreg(struct i2c_client *client)
708 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
709 if (client == vino_drvdata->decoder.driver) {
710 if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
713 vino_drvdata->decoder.driver = NULL;
714 } else if (client == vino_drvdata->camera.driver) {
715 if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
718 vino_drvdata->camera.driver = NULL;
720 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
725 static struct i2c_adapter vino_i2c_adapter =
727 .name = "VINO I2C bus",
728 .id = I2C_HW_SGI_VINO,
729 .algo_data = &i2c_sgi_vino_data,
730 .client_register = &i2c_vino_client_reg,
731 .client_unregister = &i2c_vino_client_unreg,
734 static int vino_i2c_add_bus(void)
736 return i2c_sgi_add_bus(&vino_i2c_adapter);
739 static int vino_i2c_del_bus(void)
741 return i2c_del_adapter(&vino_i2c_adapter);
744 static int i2c_camera_command(unsigned int cmd, void *arg)
746 return vino_drvdata->camera.driver->
747 driver->command(vino_drvdata->camera.driver,
751 static int i2c_decoder_command(unsigned int cmd, void *arg)
753 return vino_drvdata->decoder.driver->
754 driver->command(vino_drvdata->decoder.driver,
758 /* VINO framebuffer/DMA descriptor management */
760 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
765 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
767 for (i = 0; i < count; i++) {
768 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
769 dma_unmap_single(NULL,
770 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
771 PAGE_SIZE, DMA_FROM_DEVICE);
772 free_page(fb->desc_table.virtual[i]);
775 dma_free_coherent(NULL,
776 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
777 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
779 kfree(fb->desc_table.virtual);
781 memset(fb, 0, sizeof(struct vino_framebuffer));
784 static void vino_free_buffer(struct vino_framebuffer *fb)
786 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
789 static int vino_allocate_buffer(struct vino_framebuffer *fb,
792 unsigned int count, i, j;
795 dprintk("vino_allocate_buffer():\n");
800 memset(fb, 0, sizeof(struct vino_framebuffer));
802 count = ((size / PAGE_SIZE) + 4) & ~3;
804 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
807 /* allocate memory for table with virtual (page) addresses */
808 fb->desc_table.virtual = (unsigned long *)
809 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
810 if (!fb->desc_table.virtual)
813 /* allocate memory for table with dma addresses
814 * (has space for four extra descriptors) */
815 fb->desc_table.dma_cpu =
816 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
817 sizeof(dma_addr_t), &fb->desc_table.dma,
818 GFP_KERNEL | GFP_DMA);
819 if (!fb->desc_table.dma_cpu) {
821 goto out_free_virtual;
824 /* allocate pages for the buffer and acquire the according
826 for (i = 0; i < count; i++) {
827 dma_addr_t dma_data_addr;
829 fb->desc_table.virtual[i] =
830 get_zeroed_page(GFP_KERNEL | GFP_DMA);
831 if (!fb->desc_table.virtual[i]) {
838 (void *)fb->desc_table.virtual[i],
839 PAGE_SIZE, DMA_FROM_DEVICE);
841 for (j = 0; j < VINO_PAGE_RATIO; j++) {
842 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
843 dma_data_addr + VINO_PAGE_SIZE * j;
846 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
849 /* page_count needs to be set anyway, because the descriptor table has
850 * been allocated according to this number */
851 fb->desc_table.page_count = count;
854 /* the descriptor with index i doesn't contain
855 * a valid address yet */
856 vino_free_buffer_with_count(fb, i);
861 fb->size = count * PAGE_SIZE;
862 fb->data_format = VINO_DATA_FMT_NONE;
864 /* set the dma stop-bit for the last (count+1)th descriptor */
865 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
869 kfree(fb->desc_table.virtual);
874 /* user buffers not fully implemented yet */
875 static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
879 unsigned int count, i, j;
882 dprintk("vino_prepare_user_buffer():\n");
887 memset(fb, 0, sizeof(struct vino_framebuffer));
889 count = ((size / PAGE_SIZE)) & ~3;
891 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
894 /* allocate memory for table with virtual (page) addresses */
895 fb->desc_table.virtual = (unsigned long *)
896 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
897 if (!fb->desc_table.virtual)
900 /* allocate memory for table with dma addresses
901 * (has space for four extra descriptors) */
902 fb->desc_table.dma_cpu =
903 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
904 sizeof(dma_addr_t), &fb->desc_table.dma,
905 GFP_KERNEL | GFP_DMA);
906 if (!fb->desc_table.dma_cpu) {
908 goto out_free_virtual;
911 /* allocate pages for the buffer and acquire the according
913 for (i = 0; i < count; i++) {
914 dma_addr_t dma_data_addr;
916 fb->desc_table.virtual[i] =
917 get_zeroed_page(GFP_KERNEL | GFP_DMA);
918 if (!fb->desc_table.virtual[i]) {
925 (void *)fb->desc_table.virtual[i],
926 PAGE_SIZE, DMA_FROM_DEVICE);
928 for (j = 0; j < VINO_PAGE_RATIO; j++) {
929 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
930 dma_data_addr + VINO_PAGE_SIZE * j;
933 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
936 /* page_count needs to be set anyway, because the descriptor table has
937 * been allocated according to this number */
938 fb->desc_table.page_count = count;
941 /* the descriptor with index i doesn't contain
942 * a valid address yet */
943 vino_free_buffer_with_count(fb, i);
948 fb->size = count * PAGE_SIZE;
950 /* set the dma stop-bit for the last (count+1)th descriptor */
951 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
955 kfree(fb->desc_table.virtual);
960 static void vino_sync_buffer(struct vino_framebuffer *fb)
964 dprintk("vino_sync_buffer():\n");
966 for (i = 0; i < fb->desc_table.page_count; i++)
967 dma_sync_single(NULL,
968 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
969 PAGE_SIZE, DMA_FROM_DEVICE);
972 /* Framebuffer fifo functions (need to be locked externally) */
974 static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
982 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
983 length = VINO_FRAMEBUFFER_COUNT_MAX;
988 /* returns true/false */
989 static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
994 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
995 if (f->data[i] == id)
1003 /* returns true/false */
1004 static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
1006 return (f->used == f->length);
1010 static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
1015 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
1017 if (id >= f->length) {
1018 return VINO_QUEUE_ERROR;
1021 if (vino_fifo_has_id(f, id)) {
1022 return VINO_QUEUE_ERROR;
1025 if (f->used < f->length) {
1026 f->data[f->tail] = id;
1027 f->tail = (f->tail + 1) % f->length;
1030 return VINO_QUEUE_ERROR;
1036 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
1039 *id = f->data[f->head];
1041 return VINO_QUEUE_ERROR;
1047 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
1050 *id = f->data[f->head];
1051 f->head = (f->head + 1) % f->length;
1054 return VINO_QUEUE_ERROR;
1060 /* Framebuffer queue functions */
1062 /* execute with queue_lock locked */
1063 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
1064 unsigned int length)
1069 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
1070 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
1071 for (i = 0; i < length; i++) {
1072 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
1074 vino_free_buffer(q->buffer[i]);
1075 kfree(q->buffer[i]);
1078 q->type = VINO_MEMORY_NONE;
1082 static void vino_queue_free(struct vino_framebuffer_queue *q)
1084 dprintk("vino_queue_free():\n");
1086 if (q->magic != VINO_QUEUE_MAGIC)
1088 if (q->type != VINO_MEMORY_MMAP)
1091 mutex_lock(&q->queue_mutex);
1093 vino_queue_free_with_count(q, q->length);
1095 mutex_unlock(&q->queue_mutex);
1098 static int vino_queue_init(struct vino_framebuffer_queue *q,
1099 unsigned int *length)
1104 dprintk("vino_queue_init(): length = %d\n", *length);
1106 if (q->magic == VINO_QUEUE_MAGIC) {
1107 dprintk("vino_queue_init(): queue already initialized!\n");
1111 if (q->type != VINO_MEMORY_NONE) {
1112 dprintk("vino_queue_init(): queue already initialized!\n");
1119 mutex_lock(&q->queue_mutex);
1121 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1122 *length = VINO_FRAMEBUFFER_COUNT_MAX;
1126 for (i = 0; i < *length; i++) {
1127 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1128 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1130 if (!q->buffer[i]) {
1131 dprintk("vino_queue_init(): kmalloc() failed\n");
1136 ret = vino_allocate_buffer(q->buffer[i],
1137 VINO_FRAMEBUFFER_SIZE);
1139 kfree(q->buffer[i]);
1140 dprintk("vino_queue_init(): "
1141 "vino_allocate_buffer() failed\n");
1145 q->buffer[i]->id = i;
1147 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1148 q->buffer[i - 1]->size;
1150 q->buffer[i]->offset = 0;
1153 spin_lock_init(&q->buffer[i]->state_lock);
1155 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1156 "size = %d\n", i, q->buffer[i]->offset,
1157 q->buffer[i]->size);
1161 vino_queue_free_with_count(q, i);
1164 q->length = *length;
1165 vino_fifo_init(&q->in, q->length);
1166 vino_fifo_init(&q->out, q->length);
1167 q->type = VINO_MEMORY_MMAP;
1168 q->magic = VINO_QUEUE_MAGIC;
1171 mutex_unlock(&q->queue_mutex);
1176 static struct vino_framebuffer *vino_queue_add(struct
1177 vino_framebuffer_queue *q,
1180 struct vino_framebuffer *ret = NULL;
1182 unsigned long flags;
1184 dprintk("vino_queue_add(): id = %d\n", id);
1186 if (q->magic != VINO_QUEUE_MAGIC) {
1190 spin_lock_irqsave(&q->queue_lock, flags);
1195 if (id >= q->length)
1198 /* not needed?: if (vino_fifo_full(&q->out)) {
1201 /* check that outgoing queue isn't already full
1202 * (or that it won't become full) */
1203 total = vino_fifo_get_used(&q->in) +
1204 vino_fifo_get_used(&q->out);
1205 if (total >= q->length)
1208 if (vino_fifo_enqueue(&q->in, id))
1211 ret = q->buffer[id];
1214 spin_unlock_irqrestore(&q->queue_lock, flags);
1219 static struct vino_framebuffer *vino_queue_transfer(struct
1220 vino_framebuffer_queue *q)
1222 struct vino_framebuffer *ret = NULL;
1223 struct vino_framebuffer *fb;
1225 unsigned long flags;
1227 dprintk("vino_queue_transfer():\n");
1229 if (q->magic != VINO_QUEUE_MAGIC) {
1233 spin_lock_irqsave(&q->queue_lock, flags);
1238 // now this actually removes an entry from the incoming queue
1239 if (vino_fifo_dequeue(&q->in, &id)) {
1243 dprintk("vino_queue_transfer(): id = %d\n", id);
1246 // we have already checked that the outgoing queue is not full, but...
1247 if (vino_fifo_enqueue(&q->out, id)) {
1248 printk(KERN_ERR "vino_queue_transfer(): "
1249 "outgoing queue is full, this shouldn't happen!\n");
1255 spin_unlock_irqrestore(&q->queue_lock, flags);
1260 /* returns true/false */
1261 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1265 unsigned long flags;
1267 if (q->magic != VINO_QUEUE_MAGIC) {
1271 spin_lock_irqsave(&q->queue_lock, flags);
1276 ret = vino_fifo_has_id(&q->in, id);
1279 spin_unlock_irqrestore(&q->queue_lock, flags);
1284 /* returns true/false */
1285 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1289 unsigned long flags;
1291 if (q->magic != VINO_QUEUE_MAGIC) {
1295 spin_lock_irqsave(&q->queue_lock, flags);
1300 ret = vino_fifo_has_id(&q->out, id);
1303 spin_unlock_irqrestore(&q->queue_lock, flags);
1308 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1312 unsigned long flags;
1314 if (q->magic != VINO_QUEUE_MAGIC) {
1315 return VINO_QUEUE_ERROR;
1318 spin_lock_irqsave(&q->queue_lock, flags);
1320 if (q->length == 0) {
1321 ret = VINO_QUEUE_ERROR;
1325 *used = vino_fifo_get_used(&q->in);
1328 spin_unlock_irqrestore(&q->queue_lock, flags);
1333 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1337 unsigned long flags;
1339 if (q->magic != VINO_QUEUE_MAGIC) {
1340 return VINO_QUEUE_ERROR;
1343 spin_lock_irqsave(&q->queue_lock, flags);
1345 if (q->length == 0) {
1346 ret = VINO_QUEUE_ERROR;
1350 *used = vino_fifo_get_used(&q->out);
1353 spin_unlock_irqrestore(&q->queue_lock, flags);
1359 static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1360 unsigned int *total)
1363 unsigned long flags;
1365 if (q->magic != VINO_QUEUE_MAGIC) {
1366 return VINO_QUEUE_ERROR;
1369 spin_lock_irqsave(&q->queue_lock, flags);
1371 if (q->length == 0) {
1372 ret = VINO_QUEUE_ERROR;
1376 *total = vino_fifo_get_used(&q->in) +
1377 vino_fifo_get_used(&q->out);
1380 spin_unlock_irqrestore(&q->queue_lock, flags);
1386 static struct vino_framebuffer *vino_queue_peek(struct
1387 vino_framebuffer_queue *q,
1390 struct vino_framebuffer *ret = NULL;
1391 unsigned long flags;
1393 if (q->magic != VINO_QUEUE_MAGIC) {
1397 spin_lock_irqsave(&q->queue_lock, flags);
1402 if (vino_fifo_peek(&q->in, id)) {
1406 ret = q->buffer[*id];
1408 spin_unlock_irqrestore(&q->queue_lock, flags);
1413 static struct vino_framebuffer *vino_queue_remove(struct
1414 vino_framebuffer_queue *q,
1417 struct vino_framebuffer *ret = NULL;
1418 unsigned long flags;
1419 dprintk("vino_queue_remove():\n");
1421 if (q->magic != VINO_QUEUE_MAGIC) {
1425 spin_lock_irqsave(&q->queue_lock, flags);
1430 if (vino_fifo_dequeue(&q->out, id)) {
1434 dprintk("vino_queue_remove(): id = %d\n", *id);
1435 ret = q->buffer[*id];
1437 spin_unlock_irqrestore(&q->queue_lock, flags);
1443 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1446 struct vino_framebuffer *ret = NULL;
1447 unsigned long flags;
1449 if (q->magic != VINO_QUEUE_MAGIC) {
1453 spin_lock_irqsave(&q->queue_lock, flags);
1458 if (id >= q->length)
1461 ret = q->buffer[id];
1463 spin_unlock_irqrestore(&q->queue_lock, flags);
1468 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1470 unsigned int length = 0;
1471 unsigned long flags;
1473 if (q->magic != VINO_QUEUE_MAGIC) {
1477 spin_lock_irqsave(&q->queue_lock, flags);
1479 spin_unlock_irqrestore(&q->queue_lock, flags);
1484 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1488 unsigned long flags;
1490 if (q->magic != VINO_QUEUE_MAGIC) {
1494 spin_lock_irqsave(&q->queue_lock, flags);
1495 for (i = 0; i < q->length; i++) {
1496 if (q->buffer[i]->map_count > 0) {
1501 spin_unlock_irqrestore(&q->queue_lock, flags);
1506 /* VINO functions */
1508 /* execute with input_lock locked */
1509 static void vino_update_line_size(struct vino_channel_settings *vcs)
1511 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1512 unsigned int d = vcs->decimation;
1513 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1516 dprintk("update_line_size(): before: w = %d, d = %d, "
1517 "line_size = %d\n", w, d, vcs->line_size);
1519 /* line size must be multiple of 8 bytes */
1520 lsize = (bpp * (w / d)) & ~7;
1521 w = (lsize / bpp) * d;
1523 vcs->clipping.right = vcs->clipping.left + w;
1524 vcs->line_size = lsize;
1526 dprintk("update_line_size(): after: w = %d, d = %d, "
1527 "line_size = %d\n", w, d, vcs->line_size);
1530 /* execute with input_lock locked */
1531 static void vino_set_clipping(struct vino_channel_settings *vcs,
1532 unsigned int x, unsigned int y,
1533 unsigned int w, unsigned int h)
1535 unsigned int maxwidth, maxheight;
1538 maxwidth = vino_data_norms[vcs->data_norm].width;
1539 maxheight = vino_data_norms[vcs->data_norm].height;
1540 d = vcs->decimation;
1542 y &= ~1; /* odd/even fields */
1547 if (y > maxheight) {
1551 if (((w / d) < VINO_MIN_WIDTH)
1552 || ((h / d) < VINO_MIN_HEIGHT)) {
1553 w = VINO_MIN_WIDTH * d;
1554 h = VINO_MIN_HEIGHT * d;
1557 if ((x + w) > maxwidth) {
1559 if ((w / d) < VINO_MIN_WIDTH)
1560 x = maxwidth - VINO_MIN_WIDTH * d;
1562 if ((y + h) > maxheight) {
1564 if ((h / d) < VINO_MIN_HEIGHT)
1565 y = maxheight - VINO_MIN_HEIGHT * d;
1568 vcs->clipping.left = x;
1569 vcs->clipping.top = y;
1570 vcs->clipping.right = x + w;
1571 vcs->clipping.bottom = y + h;
1573 vino_update_line_size(vcs);
1575 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1576 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1577 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1580 /* execute with input_lock locked */
1581 static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
1583 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1584 vino_data_norms[vcs->data_norm].height);
1587 /* execute with input_lock locked */
1588 static void vino_set_scaling(struct vino_channel_settings *vcs,
1589 unsigned int w, unsigned int h)
1591 unsigned int x, y, curw, curh, d;
1593 x = vcs->clipping.left;
1594 y = vcs->clipping.top;
1595 curw = vcs->clipping.right - vcs->clipping.left;
1596 curh = vcs->clipping.bottom - vcs->clipping.top;
1598 d = max(curw / w, curh / h);
1600 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1601 w, h, curw, curh, d);
1609 vcs->decimation = d;
1610 vino_set_clipping(vcs, x, y, w * d, h * d);
1612 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1613 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1614 vcs->decimation, vcs->line_size);
1617 /* execute with input_lock locked */
1618 static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
1620 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1621 vcs->clipping.bottom - vcs->clipping.top);
1624 /* execute with input_lock locked */
1625 static void vino_set_framerate(struct vino_channel_settings *vcs,
1630 switch (vcs->data_norm) {
1631 case VINO_DATA_NORM_NTSC:
1632 case VINO_DATA_NORM_D1:
1633 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1635 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1636 fps = vino_data_norms[vcs->data_norm].fps_min;
1637 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1638 fps = vino_data_norms[vcs->data_norm].fps_max;
1657 mask = VINO_FRAMERT_FULL;
1659 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1661 case VINO_DATA_NORM_PAL:
1662 case VINO_DATA_NORM_SECAM:
1663 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1665 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1666 fps = vino_data_norms[vcs->data_norm].fps_min;
1667 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1668 fps = vino_data_norms[vcs->data_norm].fps_max;
1687 mask = VINO_FRAMERT_FULL;
1689 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1696 /* execute with input_lock locked */
1697 static inline void vino_set_default_framerate(struct
1698 vino_channel_settings *vcs)
1700 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1704 * Prepare VINO for DMA transfer...
1705 * (execute only with vino_lock and input_lock locked)
1707 static int vino_dma_setup(struct vino_channel_settings *vcs,
1708 struct vino_framebuffer *fb)
1711 struct sgi_vino_channel *ch;
1712 const struct vino_data_norm *norm;
1714 dprintk("vino_dma_setup():\n");
1717 fb->frame_counter = 0;
1719 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1720 norm = &vino_data_norms[vcs->data_norm];
1725 /* VINO line size register is set 8 bytes less than actual */
1726 ch->line_size = vcs->line_size - 8;
1728 /* let VINO know where to transfer data */
1729 ch->start_desc_tbl = fb->desc_table.dma;
1730 ch->next_4_desc = fb->desc_table.dma;
1732 /* give vino time to fetch the first four descriptors, 5 usec
1733 * should be more than enough time */
1734 udelay(VINO_DESC_FETCH_DELAY);
1736 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1737 ch->start_desc_tbl, ch->next_4_desc);
1739 /* set the alpha register */
1740 ch->alpha = vcs->alpha;
1742 /* set clipping registers */
1743 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1744 VINO_CLIP_EVEN(norm->even.top +
1745 vcs->clipping.top / 2) |
1746 VINO_CLIP_X(vcs->clipping.left);
1747 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1748 vcs->clipping.bottom / 2 - 1) |
1749 VINO_CLIP_EVEN(norm->even.top +
1750 vcs->clipping.bottom / 2 - 1) |
1751 VINO_CLIP_X(vcs->clipping.right);
1753 /* set the size of actual content in the buffer (DECIMATION !) */
1754 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1756 ((vcs->clipping.bottom - vcs->clipping.top) /
1758 vino_data_formats[vcs->data_format].bpp;
1760 ch->frame_rate = vcs->framert_reg;
1762 ctrl = vino->control;
1763 intr = vino->intr_status;
1765 if (vcs->channel == VINO_CHANNEL_A) {
1766 /* All interrupt conditions for this channel was cleared
1767 * so clear the interrupt status register and enable
1769 intr &= ~VINO_INTSTAT_A;
1770 ctrl |= VINO_CTRL_A_INT;
1772 /* enable synchronization */
1773 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1775 /* enable frame assembly */
1776 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1778 /* set decimation used */
1779 if (vcs->decimation < 2)
1780 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1782 ctrl |= VINO_CTRL_A_DEC_ENBL;
1783 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1784 ctrl |= (vcs->decimation - 1) <<
1785 VINO_CTRL_A_DEC_SCALE_SHIFT;
1788 /* select input interface */
1789 if (vcs->input == VINO_INPUT_D1)
1790 ctrl |= VINO_CTRL_A_SELECT;
1792 ctrl &= ~VINO_CTRL_A_SELECT;
1795 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1796 VINO_CTRL_A_DITHER);
1798 intr &= ~VINO_INTSTAT_B;
1799 ctrl |= VINO_CTRL_B_INT;
1801 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1802 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1804 if (vcs->decimation < 2)
1805 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1807 ctrl |= VINO_CTRL_B_DEC_ENBL;
1808 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1809 ctrl |= (vcs->decimation - 1) <<
1810 VINO_CTRL_B_DEC_SCALE_SHIFT;
1813 if (vcs->input == VINO_INPUT_D1)
1814 ctrl |= VINO_CTRL_B_SELECT;
1816 ctrl &= ~VINO_CTRL_B_SELECT;
1818 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1819 VINO_CTRL_B_DITHER);
1823 fb->data_format = vcs->data_format;
1825 switch (vcs->data_format) {
1826 case VINO_DATA_FMT_GREY:
1827 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1828 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1830 case VINO_DATA_FMT_RGB32:
1831 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1832 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1834 case VINO_DATA_FMT_YUV:
1835 /* nothing needs to be done */
1837 case VINO_DATA_FMT_RGB332:
1838 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1839 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1840 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1844 vino->intr_status = intr;
1845 vino->control = ctrl;
1850 /* (execute only with vino_lock locked) */
1851 static inline void vino_dma_start(struct vino_channel_settings *vcs)
1853 u32 ctrl = vino->control;
1855 dprintk("vino_dma_start():\n");
1856 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1857 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1858 vino->control = ctrl;
1861 /* (execute only with vino_lock locked) */
1862 static inline void vino_dma_stop(struct vino_channel_settings *vcs)
1864 u32 ctrl = vino->control;
1866 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1867 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1868 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1869 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
1870 vino->control = ctrl;
1871 dprintk("vino_dma_stop():\n");
1875 * Load dummy page to descriptor registers. This prevents generating of
1876 * spurious interrupts. (execute only with vino_lock locked)
1878 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1880 struct sgi_vino_channel *ch;
1882 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1887 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1888 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1890 udelay(VINO_DESC_FETCH_DELAY);
1891 dprintk("channel %c clear interrupt condition\n",
1892 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1895 static int vino_capture(struct vino_channel_settings *vcs,
1896 struct vino_framebuffer *fb)
1899 unsigned long flags, flags2;
1901 spin_lock_irqsave(&fb->state_lock, flags);
1903 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1905 fb->state = VINO_FRAMEBUFFER_IN_USE;
1907 spin_unlock_irqrestore(&fb->state_lock, flags);
1912 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1913 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1915 vino_dma_setup(vcs, fb);
1916 vino_dma_start(vcs);
1918 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1919 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1925 struct vino_framebuffer *vino_capture_enqueue(struct
1926 vino_channel_settings *vcs,
1929 struct vino_framebuffer *fb;
1930 unsigned long flags;
1932 dprintk("vino_capture_enqueue():\n");
1934 spin_lock_irqsave(&vcs->capture_lock, flags);
1936 fb = vino_queue_add(&vcs->fb_queue, index);
1938 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1943 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1948 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1950 struct vino_framebuffer *fb;
1951 unsigned int incoming, id;
1953 unsigned long flags;
1955 dprintk("vino_capture_next():\n");
1957 spin_lock_irqsave(&vcs->capture_lock, flags);
1960 /* start capture only if capture isn't in progress already */
1961 if (vcs->capturing) {
1962 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1967 /* capture next frame:
1968 * stop capture if capturing is not set */
1969 if (!vcs->capturing) {
1970 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1975 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
1977 dprintk("vino_capture_next(): vino_queue_get_incoming() "
1982 if (incoming == 0) {
1983 dprintk("vino_capture_next(): no buffers available\n");
1987 fb = vino_queue_peek(&vcs->fb_queue, &id);
1989 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
1998 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2000 err = vino_capture(vcs, fb);
2006 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2011 static inline int vino_is_capturing(struct vino_channel_settings *vcs)
2014 unsigned long flags;
2016 spin_lock_irqsave(&vcs->capture_lock, flags);
2018 ret = vcs->capturing;
2020 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2025 /* waits until a frame is captured */
2026 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2031 dprintk("vino_wait_for_frame():\n");
2033 init_waitqueue_entry(&wait, current);
2034 /* add ourselves into wait queue */
2035 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2037 /* to ensure that schedule_timeout will return immediately
2038 * if VINO interrupt was triggered meanwhile */
2039 schedule_timeout_interruptible(msecs_to_jiffies(100));
2041 if (signal_pending(current))
2044 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2046 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2047 err ? "failed" : "ok");
2052 /* the function assumes that PAGE_SIZE % 4 == 0 */
2053 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2054 unsigned char *pageptr;
2055 unsigned int page, i;
2058 for (page = 0; page < fb->desc_table.page_count; page++) {
2059 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2061 for (i = 0; i < PAGE_SIZE; i += 4) {
2063 pageptr[0] = pageptr[3];
2064 pageptr[1] = pageptr[2];
2065 pageptr[2] = pageptr[1];
2072 /* checks if the buffer is in correct state and syncs data */
2073 static int vino_check_buffer(struct vino_channel_settings *vcs,
2074 struct vino_framebuffer *fb)
2077 unsigned long flags;
2079 dprintk("vino_check_buffer():\n");
2081 spin_lock_irqsave(&fb->state_lock, flags);
2082 switch (fb->state) {
2083 case VINO_FRAMEBUFFER_IN_USE:
2086 case VINO_FRAMEBUFFER_READY:
2087 vino_sync_buffer(fb);
2088 fb->state = VINO_FRAMEBUFFER_UNUSED;
2093 spin_unlock_irqrestore(&fb->state_lock, flags);
2096 if (vino_pixel_conversion
2097 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2098 vino_convert_to_rgba(fb);
2100 } else if (err && (err != -EINVAL)) {
2101 dprintk("vino_check_buffer(): buffer not ready\n");
2103 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2105 vino_clear_interrupt(vcs);
2106 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2112 /* forcefully terminates capture */
2113 static void vino_capture_stop(struct vino_channel_settings *vcs)
2115 unsigned int incoming = 0, outgoing = 0, id;
2116 unsigned long flags, flags2;
2118 dprintk("vino_capture_stop():\n");
2120 spin_lock_irqsave(&vcs->capture_lock, flags);
2122 /* unset capturing to stop queue processing */
2125 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2128 vino_clear_interrupt(vcs);
2130 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2132 /* remove all items from the queue */
2133 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2134 dprintk("vino_capture_stop(): "
2135 "vino_queue_get_incoming() failed\n");
2138 while (incoming > 0) {
2139 vino_queue_transfer(&vcs->fb_queue);
2141 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2142 dprintk("vino_capture_stop(): "
2143 "vino_queue_get_incoming() failed\n");
2148 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2149 dprintk("vino_capture_stop(): "
2150 "vino_queue_get_outgoing() failed\n");
2153 while (outgoing > 0) {
2154 vino_queue_remove(&vcs->fb_queue, &id);
2156 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2157 dprintk("vino_capture_stop(): "
2158 "vino_queue_get_outgoing() failed\n");
2164 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2168 static int vino_capture_failed(struct vino_channel_settings *vcs)
2170 struct vino_framebuffer *fb;
2171 unsigned long flags;
2175 dprintk("vino_capture_failed():\n");
2177 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2180 vino_clear_interrupt(vcs);
2182 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2184 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2185 if (ret == VINO_QUEUE_ERROR) {
2186 dprintk("vino_queue_get_incoming() failed\n");
2190 /* no buffers to process */
2194 fb = vino_queue_peek(&vcs->fb_queue, &i);
2196 dprintk("vino_queue_peek() failed\n");
2200 spin_lock_irqsave(&fb->state_lock, flags);
2201 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2202 fb->state = VINO_FRAMEBUFFER_UNUSED;
2203 vino_queue_transfer(&vcs->fb_queue);
2204 vino_queue_remove(&vcs->fb_queue, &i);
2205 /* we should actually discard the newest frame,
2206 * but who cares ... */
2208 spin_unlock_irqrestore(&fb->state_lock, flags);
2214 static void vino_skip_frame(struct vino_channel_settings *vcs)
2216 struct vino_framebuffer *fb;
2217 unsigned long flags;
2220 spin_lock_irqsave(&vcs->capture_lock, flags);
2221 fb = vino_queue_peek(&vcs->fb_queue, &id);
2223 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2224 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2227 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2229 spin_lock_irqsave(&fb->state_lock, flags);
2230 fb->state = VINO_FRAMEBUFFER_UNUSED;
2231 spin_unlock_irqrestore(&fb->state_lock, flags);
2233 vino_capture_next(vcs, 0);
2236 static void vino_frame_done(struct vino_channel_settings *vcs)
2238 struct vino_framebuffer *fb;
2239 unsigned long flags;
2241 spin_lock_irqsave(&vcs->capture_lock, flags);
2242 fb = vino_queue_transfer(&vcs->fb_queue);
2244 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2245 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2248 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2250 fb->frame_counter = vcs->int_data.frame_counter;
2251 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2252 sizeof(struct timeval));
2254 spin_lock_irqsave(&fb->state_lock, flags);
2255 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2256 fb->state = VINO_FRAMEBUFFER_READY;
2257 spin_unlock_irqrestore(&fb->state_lock, flags);
2259 wake_up(&vcs->fb_queue.frame_wait_queue);
2261 vino_capture_next(vcs, 0);
2264 static void vino_capture_tasklet(unsigned long channel) {
2265 struct vino_channel_settings *vcs;
2267 vcs = (channel == VINO_CHANNEL_A)
2268 ? &vino_drvdata->a : &vino_drvdata->b;
2270 if (vcs->int_data.skip)
2271 vcs->int_data.skip_count++;
2273 if (vcs->int_data.skip && (vcs->int_data.skip_count
2274 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2275 vino_skip_frame(vcs);
2277 vcs->int_data.skip_count = 0;
2278 vino_frame_done(vcs);
2282 static irqreturn_t vino_interrupt(int irq, void *dev_id)
2285 unsigned int fc_a, fc_b;
2286 int handled_a = 0, skip_a = 0, done_a = 0;
2287 int handled_b = 0, skip_b = 0, done_b = 0;
2289 #ifdef VINO_DEBUG_INT
2291 unsigned int line_count = vino->a.line_count,
2292 page_index = vino->a.page_index,
2293 field_counter = vino->a.field_counter,
2294 start_desc_tbl = vino->a.start_desc_tbl,
2295 next_4_desc = vino->a.next_4_desc;
2296 unsigned int line_count_2,
2303 spin_lock(&vino_drvdata->vino_lock);
2305 while ((intr = vino->intr_status)) {
2306 fc_a = vino->a.field_counter >> 1;
2307 fc_b = vino->b.field_counter >> 1;
2309 /* handle error-interrupts in some special way ?
2310 * --> skips frames */
2311 if (intr & VINO_INTSTAT_A) {
2312 if (intr & VINO_INTSTAT_A_EOF) {
2313 vino_drvdata->a.field++;
2314 if (vino_drvdata->a.field > 1) {
2315 vino_dma_stop(&vino_drvdata->a);
2316 vino_clear_interrupt(&vino_drvdata->a);
2317 vino_drvdata->a.field = 0;
2320 if (vino->a.page_index
2321 != vino_drvdata->a.line_size) {
2322 vino->a.line_count = 0;
2323 vino->a.page_index =
2326 vino->a.next_4_desc =
2327 vino->a.start_desc_tbl;
2330 dprintk("channel A end-of-field "
2331 "interrupt: %04x\n", intr);
2333 vino_dma_stop(&vino_drvdata->a);
2334 vino_clear_interrupt(&vino_drvdata->a);
2335 vino_drvdata->a.field = 0;
2337 dprintk("channel A error interrupt: %04x\n",
2341 #ifdef VINO_DEBUG_INT
2342 line_count_2 = vino->a.line_count;
2343 page_index_2 = vino->a.page_index;
2344 field_counter_2 = vino->a.field_counter;
2345 start_desc_tbl_2 = vino->a.start_desc_tbl;
2346 next_4_desc_2 = vino->a.next_4_desc;
2348 printk("intr = %04x, loop = %d, field = %d\n",
2349 intr, loop, vino_drvdata->a.field);
2350 printk("1- line count = %04d, page index = %04d, "
2351 "start = %08x, next = %08x\n"
2352 " fieldc = %d, framec = %d\n",
2353 line_count, page_index, start_desc_tbl,
2354 next_4_desc, field_counter, fc_a);
2355 printk("12-line count = %04d, page index = %04d, "
2356 " start = %08x, next = %08x\n",
2357 line_count_2, page_index_2, start_desc_tbl_2,
2365 if (intr & VINO_INTSTAT_B) {
2366 if (intr & VINO_INTSTAT_B_EOF) {
2367 vino_drvdata->b.field++;
2368 if (vino_drvdata->b.field > 1) {
2369 vino_dma_stop(&vino_drvdata->b);
2370 vino_clear_interrupt(&vino_drvdata->b);
2371 vino_drvdata->b.field = 0;
2374 dprintk("channel B end-of-field "
2375 "interrupt: %04x\n", intr);
2377 vino_dma_stop(&vino_drvdata->b);
2378 vino_clear_interrupt(&vino_drvdata->b);
2379 vino_drvdata->b.field = 0;
2381 dprintk("channel B error interrupt: %04x\n",
2386 /* Always remember to clear interrupt status.
2387 * Disable VINO interrupts while we do this. */
2388 ctrl = vino->control;
2389 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2390 vino->intr_status = ~intr;
2391 vino->control = ctrl;
2393 spin_unlock(&vino_drvdata->vino_lock);
2395 if ((!handled_a) && (done_a || skip_a)) {
2397 do_gettimeofday(&vino_drvdata->
2398 a.int_data.timestamp);
2399 vino_drvdata->a.int_data.frame_counter = fc_a;
2401 vino_drvdata->a.int_data.skip = skip_a;
2403 dprintk("channel A %s, interrupt: %d\n",
2404 skip_a ? "skipping frame" : "frame done",
2406 tasklet_hi_schedule(&vino_tasklet_a);
2410 if ((!handled_b) && (done_b || skip_b)) {
2412 do_gettimeofday(&vino_drvdata->
2413 b.int_data.timestamp);
2414 vino_drvdata->b.int_data.frame_counter = fc_b;
2416 vino_drvdata->b.int_data.skip = skip_b;
2418 dprintk("channel B %s, interrupt: %d\n",
2419 skip_b ? "skipping frame" : "frame done",
2421 tasklet_hi_schedule(&vino_tasklet_b);
2425 #ifdef VINO_DEBUG_INT
2428 spin_lock(&vino_drvdata->vino_lock);
2431 spin_unlock(&vino_drvdata->vino_lock);
2436 /* VINO video input management */
2438 static int vino_get_saa7191_input(int input)
2441 case VINO_INPUT_COMPOSITE:
2442 return SAA7191_INPUT_COMPOSITE;
2443 case VINO_INPUT_SVIDEO:
2444 return SAA7191_INPUT_SVIDEO;
2446 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2447 "invalid input!\n");
2452 /* execute with input_lock locked */
2453 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2455 switch(vcs->input) {
2456 case VINO_INPUT_COMPOSITE:
2457 case VINO_INPUT_SVIDEO:
2458 return (vino_drvdata->decoder.owner == vcs->channel);
2460 return (vino_drvdata->camera.owner == vcs->channel);
2466 static int vino_acquire_input(struct vino_channel_settings *vcs)
2468 unsigned long flags;
2471 dprintk("vino_acquire_input():\n");
2473 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2475 /* First try D1 and then SAA7191 */
2476 if (vino_drvdata->camera.driver
2477 && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
2478 i2c_use_client(vino_drvdata->camera.driver);
2479 vino_drvdata->camera.owner = vcs->channel;
2480 vcs->input = VINO_INPUT_D1;
2481 vcs->data_norm = VINO_DATA_NORM_D1;
2482 } else if (vino_drvdata->decoder.driver
2483 && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
2487 struct v4l2_routing route = { 0, 0 };
2489 i2c_use_client(vino_drvdata->decoder.driver);
2490 input = VINO_INPUT_COMPOSITE;
2492 route.input = vino_get_saa7191_input(input);
2493 ret = i2c_decoder_command(VIDIOC_INT_S_VIDEO_ROUTING, &route);
2499 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2501 /* Don't hold spinlocks while auto-detecting norm
2502 * as it may take a while... */
2504 ret = i2c_decoder_command(VIDIOC_QUERYSTD, &norm);
2506 for (data_norm = 0; data_norm < 3; data_norm++) {
2507 if (vino_data_norms[data_norm].std & norm)
2511 data_norm = VINO_DATA_NORM_PAL;
2512 ret = i2c_decoder_command(VIDIOC_S_STD, &norm);
2515 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2522 vino_drvdata->decoder.owner = vcs->channel;
2525 vcs->data_norm = data_norm;
2527 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2528 vino_drvdata->b.input : vino_drvdata->a.input;
2529 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2530 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2533 if (vcs->input == VINO_INPUT_NONE) {
2538 vino_set_default_clipping(vcs);
2539 vino_set_default_scaling(vcs);
2540 vino_set_default_framerate(vcs);
2542 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2545 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2550 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2552 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2553 &vino_drvdata->b : &vino_drvdata->a;
2554 unsigned long flags;
2557 dprintk("vino_set_input():\n");
2559 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2561 if (vcs->input == input)
2565 case VINO_INPUT_COMPOSITE:
2566 case VINO_INPUT_SVIDEO:
2567 if (!vino_drvdata->decoder.driver) {
2572 if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
2573 i2c_use_client(vino_drvdata->decoder.driver);
2574 vino_drvdata->decoder.owner = vcs->channel;
2577 if (vino_drvdata->decoder.owner == vcs->channel) {
2580 struct v4l2_routing route = { 0, 0 };
2582 route.input = vino_get_saa7191_input(input);
2583 ret = i2c_decoder_command(VIDIOC_INT_S_VIDEO_ROUTING, &route);
2585 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2590 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2592 /* Don't hold spinlocks while auto-detecting norm
2593 * as it may take a while... */
2595 ret = i2c_decoder_command(VIDIOC_QUERYSTD, &norm);
2597 for (data_norm = 0; data_norm < 3; data_norm++) {
2598 if (vino_data_norms[data_norm].std & norm)
2602 data_norm = VINO_DATA_NORM_PAL;
2603 ret = i2c_decoder_command(VIDIOC_S_STD, &norm);
2606 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2609 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2615 vcs->data_norm = data_norm;
2617 if (input != vcs2->input) {
2623 vcs->data_norm = vcs2->data_norm;
2626 if (vino_drvdata->camera.owner == vcs->channel) {
2627 /* Transfer the ownership or release the input */
2628 if (vcs2->input == VINO_INPUT_D1) {
2629 vino_drvdata->camera.owner = vcs2->channel;
2631 i2c_release_client(vino_drvdata->camera.driver);
2632 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2637 if (!vino_drvdata->camera.driver) {
2642 if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
2643 i2c_use_client(vino_drvdata->camera.driver);
2644 vino_drvdata->camera.owner = vcs->channel;
2647 if (vino_drvdata->decoder.owner == vcs->channel) {
2648 /* Transfer the ownership or release the input */
2649 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2650 (vcs2->input == VINO_INPUT_SVIDEO)) {
2651 vino_drvdata->decoder.owner = vcs2->channel;
2653 i2c_release_client(vino_drvdata->
2655 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2660 vcs->data_norm = VINO_DATA_NORM_D1;
2667 vino_set_default_clipping(vcs);
2668 vino_set_default_scaling(vcs);
2669 vino_set_default_framerate(vcs);
2671 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2674 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2679 static void vino_release_input(struct vino_channel_settings *vcs)
2681 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2682 &vino_drvdata->b : &vino_drvdata->a;
2683 unsigned long flags;
2685 dprintk("vino_release_input():\n");
2687 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2689 /* Release ownership of the channel
2690 * and if the other channel takes input from
2691 * the same source, transfer the ownership */
2692 if (vino_drvdata->camera.owner == vcs->channel) {
2693 if (vcs2->input == VINO_INPUT_D1) {
2694 vino_drvdata->camera.owner = vcs2->channel;
2696 i2c_release_client(vino_drvdata->camera.driver);
2697 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2699 } else if (vino_drvdata->decoder.owner == vcs->channel) {
2700 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2701 (vcs2->input == VINO_INPUT_SVIDEO)) {
2702 vino_drvdata->decoder.owner = vcs2->channel;
2704 i2c_release_client(vino_drvdata->decoder.driver);
2705 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2708 vcs->input = VINO_INPUT_NONE;
2710 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2713 /* execute with input_lock locked */
2714 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2715 unsigned int data_norm,
2716 unsigned long *flags)
2720 if (data_norm == vcs->data_norm)
2723 switch (vcs->input) {
2725 /* only one "norm" supported */
2726 if (data_norm != VINO_DATA_NORM_D1)
2729 case VINO_INPUT_COMPOSITE:
2730 case VINO_INPUT_SVIDEO: {
2733 if ((data_norm != VINO_DATA_NORM_PAL)
2734 && (data_norm != VINO_DATA_NORM_NTSC)
2735 && (data_norm != VINO_DATA_NORM_SECAM))
2738 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
2740 /* Don't hold spinlocks while setting norm
2741 * as it may take a while... */
2743 norm = vino_data_norms[data_norm].std;
2744 err = i2c_decoder_command(VIDIOC_S_STD, &norm);
2746 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2751 vcs->data_norm = data_norm;
2753 vino_set_default_clipping(vcs);
2754 vino_set_default_scaling(vcs);
2755 vino_set_default_framerate(vcs);
2766 /* V4L2 helper functions */
2768 static int vino_find_data_format(__u32 pixelformat)
2772 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2773 if (vino_data_formats[i].pixelformat == pixelformat)
2777 return VINO_DATA_FMT_NONE;
2780 static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
2782 int input = VINO_INPUT_NONE;
2783 unsigned long flags;
2785 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2786 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2789 input = VINO_INPUT_COMPOSITE;
2792 input = VINO_INPUT_SVIDEO;
2795 input = VINO_INPUT_D1;
2798 } else if (vino_drvdata->decoder.driver) {
2801 input = VINO_INPUT_COMPOSITE;
2804 input = VINO_INPUT_SVIDEO;
2807 } else if (vino_drvdata->camera.driver) {
2810 input = VINO_INPUT_D1;
2814 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2819 /* execute with input_lock locked */
2820 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2823 // FIXME: detect when no inputs available
2825 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2826 switch (vcs->input) {
2827 case VINO_INPUT_COMPOSITE:
2830 case VINO_INPUT_SVIDEO:
2837 } else if (vino_drvdata->decoder.driver) {
2838 switch (vcs->input) {
2839 case VINO_INPUT_COMPOSITE:
2842 case VINO_INPUT_SVIDEO:
2846 } else if (vino_drvdata->camera.driver) {
2847 switch (vcs->input) {
2859 static int vino_querycap(struct file *file, void *__fh,
2860 struct v4l2_capability *cap)
2862 memset(cap, 0, sizeof(struct v4l2_capability));
2864 strcpy(cap->driver, vino_driver_name);
2865 strcpy(cap->card, vino_driver_description);
2866 strcpy(cap->bus_info, vino_bus_name);
2867 cap->version = VINO_VERSION_CODE;
2869 V4L2_CAP_VIDEO_CAPTURE |
2871 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2875 static int vino_enum_input(struct file *file, void *__fh,
2876 struct v4l2_input *i)
2878 struct vino_channel_settings *vcs = video_drvdata(file);
2879 __u32 index = i->index;
2881 dprintk("requested index = %d\n", index);
2883 input = vino_int_enum_input(vcs, index);
2884 if (input == VINO_INPUT_NONE)
2887 memset(i, 0, sizeof(struct v4l2_input));
2890 i->type = V4L2_INPUT_TYPE_CAMERA;
2891 i->std = vino_inputs[input].std;
2892 strcpy(i->name, vino_inputs[input].name);
2894 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2895 i2c_decoder_command(VIDIOC_INT_G_INPUT_STATUS, &i->status);
2899 static int vino_g_input(struct file *file, void *__fh,
2902 struct vino_channel_settings *vcs = video_drvdata(file);
2905 unsigned long flags;
2907 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2909 index = vino_find_input_index(vcs);
2910 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2912 dprintk("input = %d\n", input);
2914 if (input == VINO_INPUT_NONE) {
2923 static int vino_s_input(struct file *file, void *__fh,
2926 struct vino_channel_settings *vcs = video_drvdata(file);
2928 dprintk("requested input = %d\n", i);
2930 input = vino_int_enum_input(vcs, i);
2931 if (input == VINO_INPUT_NONE)
2934 return vino_set_input(vcs, input);
2937 static int vino_querystd(struct file *file, void *__fh,
2940 struct vino_channel_settings *vcs = video_drvdata(file);
2941 unsigned long flags;
2944 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2946 switch (vcs->input) {
2948 *std = vino_inputs[vcs->input].std;
2950 case VINO_INPUT_COMPOSITE:
2951 case VINO_INPUT_SVIDEO: {
2952 i2c_decoder_command(VIDIOC_QUERYSTD, std);
2959 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2964 static int vino_g_std(struct file *file, void *__fh,
2967 struct vino_channel_settings *vcs = video_drvdata(file);
2968 unsigned long flags;
2970 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2972 *std = vino_data_norms[vcs->data_norm].std;
2973 dprintk("current standard = %d\n", vcs->data_norm);
2975 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2980 static int vino_s_std(struct file *file, void *__fh,
2983 struct vino_channel_settings *vcs = video_drvdata(file);
2984 unsigned long flags;
2987 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2989 if (!vino_is_input_owner(vcs)) {
2994 /* check if the standard is valid for the current input */
2995 if ((*std) & vino_inputs[vcs->input].std) {
2996 dprintk("standard accepted\n");
2998 /* change the video norm for SAA7191
2999 * and accept NTSC for D1 (do nothing) */
3001 if (vcs->input == VINO_INPUT_D1)
3004 if ((*std) & V4L2_STD_PAL) {
3005 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3007 } else if ((*std) & V4L2_STD_NTSC) {
3008 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3010 } else if ((*std) & V4L2_STD_SECAM) {
3011 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3025 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3030 static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
3031 struct v4l2_fmtdesc *fd)
3033 enum v4l2_buf_type type = fd->type;
3034 int index = fd->index;
3036 dprintk("format index = %d\n", index);
3038 if ((fd->index < 0) ||
3039 (fd->index >= VINO_DATA_FMT_COUNT))
3041 dprintk("format name = %s\n",
3042 vino_data_formats[index].description);
3044 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3047 fd->pixelformat = vino_data_formats[index].pixelformat;
3048 strcpy(fd->description, vino_data_formats[index].description);
3052 static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
3053 struct v4l2_format *f)
3055 struct vino_channel_settings *vcs = video_drvdata(file);
3056 struct vino_channel_settings tempvcs;
3057 unsigned long flags;
3058 struct v4l2_pix_format *pf = &f->fmt.pix;
3060 dprintk("requested: w = %d, h = %d\n",
3061 pf->width, pf->height);
3063 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3064 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3065 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3067 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3068 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3069 tempvcs.data_format = VINO_DATA_FMT_GREY;
3071 vino_data_formats[tempvcs.data_format].
3075 /* data format must be set before clipping/scaling */
3076 vino_set_scaling(&tempvcs, pf->width, pf->height);
3078 dprintk("data format = %s\n",
3079 vino_data_formats[tempvcs.data_format].description);
3081 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3083 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3086 pf->field = V4L2_FIELD_INTERLACED;
3087 pf->bytesperline = tempvcs.line_size;
3088 pf->sizeimage = tempvcs.line_size *
3089 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3092 vino_data_formats[tempvcs.data_format].colorspace;
3098 static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
3099 struct v4l2_format *f)
3101 struct vino_channel_settings *vcs = video_drvdata(file);
3102 unsigned long flags;
3103 struct v4l2_pix_format *pf = &f->fmt.pix;
3105 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3107 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3109 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3112 vino_data_formats[vcs->data_format].pixelformat;
3114 pf->field = V4L2_FIELD_INTERLACED;
3115 pf->bytesperline = vcs->line_size;
3116 pf->sizeimage = vcs->line_size *
3117 (vcs->clipping.bottom - vcs->clipping.top) /
3120 vino_data_formats[vcs->data_format].colorspace;
3124 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3128 static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3129 struct v4l2_format *f)
3131 struct vino_channel_settings *vcs = video_drvdata(file);
3133 unsigned long flags;
3134 struct v4l2_pix_format *pf = &f->fmt.pix;
3136 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3138 data_format = vino_find_data_format(pf->pixelformat);
3140 if (data_format == VINO_DATA_FMT_NONE) {
3141 vcs->data_format = VINO_DATA_FMT_GREY;
3143 vino_data_formats[vcs->data_format].
3146 vcs->data_format = data_format;
3149 /* data format must be set before clipping/scaling */
3150 vino_set_scaling(vcs, pf->width, pf->height);
3152 dprintk("data format = %s\n",
3153 vino_data_formats[vcs->data_format].description);
3155 pf->width = vcs->clipping.right - vcs->clipping.left;
3156 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3158 pf->field = V4L2_FIELD_INTERLACED;
3159 pf->bytesperline = vcs->line_size;
3160 pf->sizeimage = vcs->line_size *
3161 (vcs->clipping.bottom - vcs->clipping.top) /
3164 vino_data_formats[vcs->data_format].colorspace;
3168 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3172 static int vino_cropcap(struct file *file, void *__fh,
3173 struct v4l2_cropcap *ccap)
3175 struct vino_channel_settings *vcs = video_drvdata(file);
3176 const struct vino_data_norm *norm;
3177 unsigned long flags;
3179 switch (ccap->type) {
3180 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3181 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3183 norm = &vino_data_norms[vcs->data_norm];
3185 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3187 ccap->bounds.left = 0;
3188 ccap->bounds.top = 0;
3189 ccap->bounds.width = norm->width;
3190 ccap->bounds.height = norm->height;
3191 memcpy(&ccap->defrect, &ccap->bounds,
3192 sizeof(struct v4l2_rect));
3194 ccap->pixelaspect.numerator = 1;
3195 ccap->pixelaspect.denominator = 1;
3197 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3205 static int vino_g_crop(struct file *file, void *__fh,
3206 struct v4l2_crop *c)
3208 struct vino_channel_settings *vcs = video_drvdata(file);
3209 unsigned long flags;
3212 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3213 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3215 c->c.left = vcs->clipping.left;
3216 c->c.top = vcs->clipping.top;
3217 c->c.width = vcs->clipping.right - vcs->clipping.left;
3218 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3220 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3222 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3230 static int vino_s_crop(struct file *file, void *__fh,
3231 struct v4l2_crop *c)
3233 struct vino_channel_settings *vcs = video_drvdata(file);
3234 unsigned long flags;
3237 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3238 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3240 vino_set_clipping(vcs, c->c.left, c->c.top,
3241 c->c.width, c->c.height);
3243 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3245 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3253 static int vino_g_parm(struct file *file, void *__fh,
3254 struct v4l2_streamparm *sp)
3256 struct vino_channel_settings *vcs = video_drvdata(file);
3257 unsigned long flags;
3260 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3261 struct v4l2_captureparm *cp = &sp->parm.capture;
3262 memset(cp, 0, sizeof(struct v4l2_captureparm));
3264 cp->capability = V4L2_CAP_TIMEPERFRAME;
3265 cp->timeperframe.numerator = 1;
3267 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3269 cp->timeperframe.denominator = vcs->fps;
3271 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3273 // TODO: cp->readbuffers = xxx;
3276 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3284 static int vino_s_parm(struct file *file, void *__fh,
3285 struct v4l2_streamparm *sp)
3287 struct vino_channel_settings *vcs = video_drvdata(file);
3288 unsigned long flags;
3291 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3292 struct v4l2_captureparm *cp = &sp->parm.capture;
3294 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3296 if ((cp->timeperframe.numerator == 0) ||
3297 (cp->timeperframe.denominator == 0)) {
3298 /* reset framerate */
3299 vino_set_default_framerate(vcs);
3301 vino_set_framerate(vcs, cp->timeperframe.denominator /
3302 cp->timeperframe.numerator);
3305 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3307 // TODO: set buffers according to cp->readbuffers
3310 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3318 static int vino_reqbufs(struct file *file, void *__fh,
3319 struct v4l2_requestbuffers *rb)
3321 struct vino_channel_settings *vcs = video_drvdata(file);
3326 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3327 // TODO: check queue type
3328 if (rb->memory != V4L2_MEMORY_MMAP) {
3329 dprintk("type not mmap\n");
3333 dprintk("count = %d\n", rb->count);
3334 if (rb->count > 0) {
3335 if (vino_is_capturing(vcs)) {
3336 dprintk("busy, capturing\n");
3340 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3341 dprintk("busy, buffers still mapped\n");
3345 vino_queue_free(&vcs->fb_queue);
3346 vino_queue_init(&vcs->fb_queue, &rb->count);
3350 vino_capture_stop(vcs);
3351 vino_queue_free(&vcs->fb_queue);
3355 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3363 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3364 struct vino_framebuffer *fb,
3365 struct v4l2_buffer *b)
3367 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3369 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3370 b->flags |= V4L2_BUF_FLAG_DONE;
3371 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3373 b->flags &= ~V4L2_BUF_FLAG_DONE;
3374 b->flags |= V4L2_BUF_FLAG_QUEUED;
3376 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3377 V4L2_BUF_FLAG_QUEUED);
3380 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3382 if (fb->map_count > 0)
3383 b->flags |= V4L2_BUF_FLAG_MAPPED;
3386 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3387 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3388 b->m.offset = fb->offset;
3389 b->bytesused = fb->data_size;
3390 b->length = fb->size;
3391 b->field = V4L2_FIELD_INTERLACED;
3392 b->sequence = fb->frame_counter;
3393 memcpy(&b->timestamp, &fb->timestamp,
3394 sizeof(struct timeval));
3397 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3398 fb->id, fb->size, fb->data_size, fb->offset);
3401 static int vino_querybuf(struct file *file, void *__fh,
3402 struct v4l2_buffer *b)
3404 struct vino_channel_settings *vcs = video_drvdata(file);
3409 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3410 struct vino_framebuffer *fb;
3412 // TODO: check queue type
3413 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3414 dprintk("invalid index = %d\n",
3419 fb = vino_queue_get_buffer(&vcs->fb_queue,
3422 dprintk("vino_queue_get_buffer() failed");
3426 vino_v4l2_get_buffer_status(vcs, fb, b);
3429 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3437 static int vino_qbuf(struct file *file, void *__fh,
3438 struct v4l2_buffer *b)
3440 struct vino_channel_settings *vcs = video_drvdata(file);
3445 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3446 struct vino_framebuffer *fb;
3449 // TODO: check queue type
3450 if (b->memory != V4L2_MEMORY_MMAP) {
3451 dprintk("type not mmap\n");
3455 fb = vino_capture_enqueue(vcs, b->index);
3459 vino_v4l2_get_buffer_status(vcs, fb, b);
3461 if (vcs->streaming) {
3462 ret = vino_capture_next(vcs, 1);
3468 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3476 static int vino_dqbuf(struct file *file, void *__fh,
3477 struct v4l2_buffer *b)
3479 struct vino_channel_settings *vcs = video_drvdata(file);
3480 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3485 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3486 struct vino_framebuffer *fb;
3487 unsigned int incoming, outgoing;
3490 // TODO: check queue type
3492 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3494 dprintk("vino_queue_get_incoming() failed\n");
3497 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3499 dprintk("vino_queue_get_outgoing() failed\n");
3503 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3505 if (outgoing == 0) {
3506 if (incoming == 0) {
3507 dprintk("no incoming or outgoing buffers\n");
3511 dprintk("non-blocking I/O was selected and "
3512 "there are no buffers to dequeue\n");
3516 err = vino_wait_for_frame(vcs);
3518 err = vino_wait_for_frame(vcs);
3521 * no frames captured because
3522 * of frame skipping */
3523 // vino_capture_failed(vcs);
3529 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3531 dprintk("vino_queue_remove() failed\n");
3535 err = vino_check_buffer(vcs, fb);
3537 vino_v4l2_get_buffer_status(vcs, fb, b);
3544 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3552 static int vino_streamon(struct file *file, void *__fh,
3553 enum v4l2_buf_type i)
3555 struct vino_channel_settings *vcs = video_drvdata(file);
3556 unsigned int incoming;
3564 // TODO: check queue type
3566 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3567 dprintk("no buffers allocated\n");
3571 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3573 dprintk("vino_queue_get_incoming() failed\n");
3580 ret = vino_capture_next(vcs, 1);
3584 dprintk("couldn't start capture\n");
3592 static int vino_streamoff(struct file *file, void *__fh,
3593 enum v4l2_buf_type i)
3595 struct vino_channel_settings *vcs = video_drvdata(file);
3599 if (!vcs->streaming)
3603 vino_capture_stop(vcs);
3608 static int vino_queryctrl(struct file *file, void *__fh,
3609 struct v4l2_queryctrl *queryctrl)
3611 struct vino_channel_settings *vcs = video_drvdata(file);
3612 unsigned long flags;
3616 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3618 switch (vcs->input) {
3620 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3621 if (vino_indycam_v4l2_controls[i].id ==
3624 &vino_indycam_v4l2_controls[i],
3625 sizeof(struct v4l2_queryctrl));
3626 queryctrl->reserved[0] = 0;
3633 case VINO_INPUT_COMPOSITE:
3634 case VINO_INPUT_SVIDEO:
3635 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3636 if (vino_saa7191_v4l2_controls[i].id ==
3639 &vino_saa7191_v4l2_controls[i],
3640 sizeof(struct v4l2_queryctrl));
3641 queryctrl->reserved[0] = 0;
3653 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3658 static int vino_g_ctrl(struct file *file, void *__fh,
3659 struct v4l2_control *control)
3661 struct vino_channel_settings *vcs = video_drvdata(file);
3662 unsigned long flags;
3666 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3668 switch (vcs->input) {
3669 case VINO_INPUT_D1: {
3671 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3672 if (vino_indycam_v4l2_controls[i].id == control->id) {
3681 err = i2c_camera_command(VIDIOC_G_CTRL, &control);
3686 case VINO_INPUT_COMPOSITE:
3687 case VINO_INPUT_SVIDEO: {
3689 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3690 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3699 err = i2c_decoder_command(VIDIOC_G_CTRL, &control);
3709 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3714 static int vino_s_ctrl(struct file *file, void *__fh,
3715 struct v4l2_control *control)
3717 struct vino_channel_settings *vcs = video_drvdata(file);
3718 unsigned long flags;
3722 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3724 if (!vino_is_input_owner(vcs)) {
3729 switch (vcs->input) {
3730 case VINO_INPUT_D1: {
3732 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3733 if (vino_indycam_v4l2_controls[i].id == control->id) {
3740 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3741 control->value > vino_indycam_v4l2_controls[i].maximum) {
3745 err = i2c_camera_command(VIDIOC_S_CTRL, &control);
3750 case VINO_INPUT_COMPOSITE:
3751 case VINO_INPUT_SVIDEO: {
3753 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3754 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3761 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3762 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3767 err = i2c_decoder_command(VIDIOC_S_CTRL, &control);
3777 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3782 /* File operations */
3784 static int vino_open(struct file *file)
3786 struct vino_channel_settings *vcs = video_drvdata(file);
3788 dprintk("open(): channel = %c\n",
3789 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3791 mutex_lock(&vcs->mutex);
3794 dprintk("open(): driver busy\n");
3799 ret = vino_acquire_input(vcs);
3801 dprintk("open(): vino_acquire_input() failed\n");
3808 mutex_unlock(&vcs->mutex);
3810 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3815 static int vino_close(struct file *file)
3817 struct vino_channel_settings *vcs = video_drvdata(file);
3818 dprintk("close():\n");
3820 mutex_lock(&vcs->mutex);
3825 vino_release_input(vcs);
3827 /* stop DMA and free buffers */
3828 vino_capture_stop(vcs);
3829 vino_queue_free(&vcs->fb_queue);
3832 mutex_unlock(&vcs->mutex);
3837 static void vino_vm_open(struct vm_area_struct *vma)
3839 struct vino_framebuffer *fb = vma->vm_private_data;
3842 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3845 static void vino_vm_close(struct vm_area_struct *vma)
3847 struct vino_framebuffer *fb = vma->vm_private_data;
3850 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3853 static struct vm_operations_struct vino_vm_ops = {
3854 .open = vino_vm_open,
3855 .close = vino_vm_close,
3858 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3860 struct vino_channel_settings *vcs = video_drvdata(file);
3862 unsigned long start = vma->vm_start;
3863 unsigned long size = vma->vm_end - vma->vm_start;
3864 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3866 struct vino_framebuffer *fb = NULL;
3867 unsigned int i, length;
3870 dprintk("mmap():\n");
3872 // TODO: reject mmap if already mapped
3874 if (mutex_lock_interruptible(&vcs->mutex))
3882 // TODO: check queue type
3884 if (!(vma->vm_flags & VM_WRITE)) {
3885 dprintk("mmap(): app bug: PROT_WRITE please\n");
3889 if (!(vma->vm_flags & VM_SHARED)) {
3890 dprintk("mmap(): app bug: MAP_SHARED please\n");
3895 /* find the correct buffer using offset */
3896 length = vino_queue_get_length(&vcs->fb_queue);
3898 dprintk("mmap(): queue not initialized\n");
3903 for (i = 0; i < length; i++) {
3904 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3906 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3911 if (fb->offset == offset)
3915 dprintk("mmap(): invalid offset = %lu\n", offset);
3920 dprintk("mmap(): buffer = %d\n", i);
3922 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3923 dprintk("mmap(): failed: size = %lu > %lu\n",
3924 size, fb->desc_table.page_count * PAGE_SIZE);
3929 for (i = 0; i < fb->desc_table.page_count; i++) {
3931 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3934 if (size < PAGE_SIZE)
3937 // protection was: PAGE_READONLY
3938 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3939 vma->vm_page_prot)) {
3940 dprintk("mmap(): remap_pfn_range() failed\n");
3951 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3952 vma->vm_flags &= ~VM_IO;
3953 vma->vm_private_data = fb;
3954 vma->vm_file = file;
3955 vma->vm_ops = &vino_vm_ops;
3958 mutex_unlock(&vcs->mutex);
3963 static unsigned int vino_poll(struct file *file, poll_table *pt)
3965 struct vino_channel_settings *vcs = video_drvdata(file);
3966 unsigned int outgoing;
3967 unsigned int ret = 0;
3970 // TODO: this has to be corrected for different read modes
3972 dprintk("poll():\n");
3974 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3975 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3982 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3984 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3985 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3991 dprintk("poll(): data %savailable\n",
3992 (outgoing > 0) ? "" : "not ");
3995 ret = POLLIN | POLLRDNORM;
4002 static long vino_ioctl(struct file *file,
4003 unsigned int cmd, unsigned long arg)
4005 struct vino_channel_settings *vcs = video_drvdata(file);
4008 if (mutex_lock_interruptible(&vcs->mutex))
4011 ret = video_ioctl2(file, cmd, arg);
4013 mutex_unlock(&vcs->mutex);
4018 /* Initialization and cleanup */
4021 static int vino_init_stage;
4023 const struct v4l2_ioctl_ops vino_ioctl_ops = {
4024 .vidioc_enum_fmt_vid_cap = vino_enum_fmt_vid_cap,
4025 .vidioc_g_fmt_vid_cap = vino_g_fmt_vid_cap,
4026 .vidioc_s_fmt_vid_cap = vino_s_fmt_vid_cap,
4027 .vidioc_try_fmt_vid_cap = vino_try_fmt_vid_cap,
4028 .vidioc_querycap = vino_querycap,
4029 .vidioc_enum_input = vino_enum_input,
4030 .vidioc_g_input = vino_g_input,
4031 .vidioc_s_input = vino_s_input,
4032 .vidioc_g_std = vino_g_std,
4033 .vidioc_s_std = vino_s_std,
4034 .vidioc_querystd = vino_querystd,
4035 .vidioc_cropcap = vino_cropcap,
4036 .vidioc_s_crop = vino_s_crop,
4037 .vidioc_g_crop = vino_g_crop,
4038 .vidioc_s_parm = vino_s_parm,
4039 .vidioc_g_parm = vino_g_parm,
4040 .vidioc_reqbufs = vino_reqbufs,
4041 .vidioc_querybuf = vino_querybuf,
4042 .vidioc_qbuf = vino_qbuf,
4043 .vidioc_dqbuf = vino_dqbuf,
4044 .vidioc_streamon = vino_streamon,
4045 .vidioc_streamoff = vino_streamoff,
4046 .vidioc_queryctrl = vino_queryctrl,
4047 .vidioc_g_ctrl = vino_g_ctrl,
4048 .vidioc_s_ctrl = vino_s_ctrl,
4051 static const struct v4l2_file_operations vino_fops = {
4052 .owner = THIS_MODULE,
4054 .release = vino_close,
4055 .ioctl = vino_ioctl,
4060 static struct video_device vdev_template = {
4063 .ioctl_ops = &vino_ioctl_ops,
4064 .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
4068 static void vino_module_cleanup(int stage)
4072 video_unregister_device(vino_drvdata->b.vdev);
4073 vino_drvdata->b.vdev = NULL;
4075 video_unregister_device(vino_drvdata->a.vdev);
4076 vino_drvdata->a.vdev = NULL;
4080 free_irq(SGI_VINO_IRQ, NULL);
4082 if (vino_drvdata->b.vdev) {
4083 video_device_release(vino_drvdata->b.vdev);
4084 vino_drvdata->b.vdev = NULL;
4087 if (vino_drvdata->a.vdev) {
4088 video_device_release(vino_drvdata->a.vdev);
4089 vino_drvdata->a.vdev = NULL;
4092 /* all entries in dma_cpu dummy table have the same address */
4093 dma_unmap_single(NULL,
4094 vino_drvdata->dummy_desc_table.dma_cpu[0],
4095 PAGE_SIZE, DMA_FROM_DEVICE);
4096 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4097 * sizeof(dma_addr_t),
4098 (void *)vino_drvdata->
4099 dummy_desc_table.dma_cpu,
4100 vino_drvdata->dummy_desc_table.dma);
4102 free_page(vino_drvdata->dummy_page);
4104 kfree(vino_drvdata);
4110 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4115 static int vino_probe(void)
4117 unsigned long rev_id;
4119 if (ip22_is_fullhouse()) {
4120 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4124 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4125 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4129 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4131 printk(KERN_ERR "VINO: ioremap() failed\n");
4136 if (get_dbe(rev_id, &(vino->rev_id))) {
4137 printk(KERN_ERR "Failed to read VINO revision register\n");
4138 vino_module_cleanup(vino_init_stage);
4142 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4143 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4145 vino_module_cleanup(vino_init_stage);
4149 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
4154 static int vino_init(void)
4156 dma_addr_t dma_dummy_address;
4159 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
4160 if (!vino_drvdata) {
4161 vino_module_cleanup(vino_init_stage);
4166 /* create a dummy dma descriptor */
4167 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4168 if (!vino_drvdata->dummy_page) {
4169 vino_module_cleanup(vino_init_stage);
4174 // TODO: use page_count in dummy_desc_table
4176 vino_drvdata->dummy_desc_table.dma_cpu =
4177 dma_alloc_coherent(NULL,
4178 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4179 &vino_drvdata->dummy_desc_table.dma,
4180 GFP_KERNEL | GFP_DMA);
4181 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4182 vino_module_cleanup(vino_init_stage);
4187 dma_dummy_address = dma_map_single(NULL,
4188 (void *)vino_drvdata->dummy_page,
4189 PAGE_SIZE, DMA_FROM_DEVICE);
4190 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4191 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4194 /* initialize VINO */
4197 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4198 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4199 udelay(VINO_DESC_FETCH_DELAY);
4201 vino->intr_status = 0;
4203 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4204 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4209 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4210 unsigned int channel, const char *name)
4212 vcs->channel = channel;
4213 vcs->input = VINO_INPUT_NONE;
4216 vcs->data_format = VINO_DATA_FMT_GREY;
4217 vcs->data_norm = VINO_DATA_NORM_NTSC;
4218 vcs->decimation = 1;
4219 vino_set_default_clipping(vcs);
4220 vino_set_default_framerate(vcs);
4224 mutex_init(&vcs->mutex);
4225 spin_lock_init(&vcs->capture_lock);
4227 mutex_init(&vcs->fb_queue.queue_mutex);
4228 spin_lock_init(&vcs->fb_queue.queue_lock);
4229 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4231 vcs->vdev = video_device_alloc();
4233 vino_module_cleanup(vino_init_stage);
4238 memcpy(vcs->vdev, &vdev_template,
4239 sizeof(struct video_device));
4240 strcpy(vcs->vdev->name, name);
4241 vcs->vdev->release = video_device_release;
4243 video_set_drvdata(vcs->vdev, vcs);
4248 static int __init vino_module_init(void)
4252 printk(KERN_INFO "SGI VINO driver version %s\n",
4253 VINO_MODULE_VERSION);
4263 /* initialize data structures */
4265 spin_lock_init(&vino_drvdata->vino_lock);
4266 spin_lock_init(&vino_drvdata->input_lock);
4268 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4273 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4278 /* initialize hardware and register V4L devices */
4280 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4281 vino_driver_description, NULL);
4283 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4285 vino_module_cleanup(vino_init_stage);
4290 ret = vino_i2c_add_bus();
4292 printk(KERN_ERR "VINO I2C bus registration failed\n");
4293 vino_module_cleanup(vino_init_stage);
4298 ret = video_register_device(vino_drvdata->a.vdev,
4299 VFL_TYPE_GRABBER, -1);
4301 printk(KERN_ERR "VINO channel A Video4Linux-device "
4302 "registration failed\n");
4303 vino_module_cleanup(vino_init_stage);
4308 ret = video_register_device(vino_drvdata->b.vdev,
4309 VFL_TYPE_GRABBER, -1);
4311 printk(KERN_ERR "VINO channel B Video4Linux-device "
4312 "registration failed\n");
4313 vino_module_cleanup(vino_init_stage);
4319 request_module("saa7191");
4320 request_module("indycam");
4323 dprintk("init complete!\n");
4328 static void __exit vino_module_exit(void)
4330 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4331 vino_module_cleanup(vino_init_stage);
4332 dprintk("cleanup complete, exit!\n");
4335 module_init(vino_module_init);
4336 module_exit(vino_module_exit);