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 "hacks" from memory allocation code and implement nopage()
16 * - check decimation, calculating and reporting image size when
18 * - check vino_acquire_input(), vino_set_input() and channel
20 * - report VINO error-interrupts via ioctls ?
21 * - implement picture controls (all implemented?)
22 * - use macros for boolean values (?)
23 * - implement user mode buffers and overlay (?)
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/errno.h>
32 #include <linux/interrupt.h>
33 #include <linux/kernel.h>
35 #include <linux/moduleparam.h>
36 #include <linux/time.h>
37 #include <linux/version.h>
40 #include <linux/kmod.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-algo-sgi.h>
46 #include <linux/videodev.h>
47 #include <linux/videodev2.h>
48 #include <linux/video_decoder.h>
50 #include <asm/paccess.h>
52 #include <asm/sgi/ip22.h>
53 #include <asm/sgi/mc.h>
59 /* Uncomment the following line to get lots and lots of (mostly useless)
61 * Note that the debug output also slows down the driver significantly */
64 #define VINO_MODULE_VERSION "0.0.3"
65 #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 3)
67 MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
68 MODULE_VERSION(VINO_MODULE_VERSION);
69 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
70 MODULE_LICENSE("GPL");
72 #define mem_map_reserve(p) set_bit(PG_reserved, &((p)->flags))
73 #define mem_map_unreserve(p) clear_bit(PG_reserved, &((p)->flags))
76 #define dprintk(x...) printk("VINO: " x);
81 #define VINO_NO_CHANNEL 0
82 #define VINO_CHANNEL_A 1
83 #define VINO_CHANNEL_B 2
85 #define VINO_PAL_WIDTH 768
86 #define VINO_PAL_HEIGHT 576
87 #define VINO_NTSC_WIDTH 640
88 #define VINO_NTSC_HEIGHT 480
90 #define VINO_MIN_WIDTH 32
91 #define VINO_MIN_HEIGHT 32
93 #define VINO_CLIPPING_START_ODD_D1 1
94 #define VINO_CLIPPING_START_ODD_PAL 1
95 #define VINO_CLIPPING_START_ODD_NTSC 1
97 #define VINO_CLIPPING_START_EVEN_D1 2
98 #define VINO_CLIPPING_START_EVEN_PAL 2
99 #define VINO_CLIPPING_START_EVEN_NTSC 2
101 #define VINO_INPUT_CHANNEL_COUNT 3
103 #define VINO_INPUT_NONE -1
104 #define VINO_INPUT_COMPOSITE 0
105 #define VINO_INPUT_SVIDEO 1
106 #define VINO_INPUT_D1 2
108 #define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
110 #define VINO_FIFO_THRESHOLD_DEFAULT 512
112 /*#define VINO_FRAMEBUFFER_SIZE (VINO_PAL_WIDTH * VINO_PAL_HEIGHT * 4 \
114 #define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
115 * VINO_PAL_HEIGHT * 4 \
116 + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
118 #define VINO_FRAMEBUFFER_MAX_COUNT 8
120 #define VINO_FRAMEBUFFER_UNUSED 0
121 #define VINO_FRAMEBUFFER_IN_USE 1
122 #define VINO_FRAMEBUFFER_READY 2
124 #define VINO_QUEUE_ERROR -1
125 #define VINO_QUEUE_MAGIC 0x20050125
127 #define VINO_MEMORY_NONE 0
128 #define VINO_MEMORY_MMAP 1
129 #define VINO_MEMORY_USERPTR 2
131 #define VINO_DUMMY_DESC_COUNT 4
132 #define VINO_DESC_FETCH_DELAY 5 /* microseconds */
134 /* the number is the index for vino_data_formats */
135 #define VINO_DATA_FMT_NONE -1
136 #define VINO_DATA_FMT_GREY 0
137 #define VINO_DATA_FMT_RGB332 1
138 #define VINO_DATA_FMT_RGB32 2
139 #define VINO_DATA_FMT_YUV 3
140 //#define VINO_DATA_FMT_RGB24 4
142 #define VINO_DATA_FMT_COUNT 4
144 #define VINO_DATA_NORM_NONE -1
145 #define VINO_DATA_NORM_NTSC 0
146 #define VINO_DATA_NORM_PAL 1
147 #define VINO_DATA_NORM_SECAM 2
148 #define VINO_DATA_NORM_D1 3
149 /* The following is a special entry that can be used to
150 * autodetect the norm. */
151 #define VINO_DATA_NORM_AUTO 0xff
153 #define VINO_DATA_NORM_COUNT 4
155 /* Internal data structure definitions */
162 struct vino_clipping {
163 unsigned int left, right, top, bottom;
166 struct vino_data_format {
167 /* the description */
169 /* bytes per pixel */
171 /* V4L2 fourcc code */
173 /* V4L2 colorspace (duh!) */
174 enum v4l2_colorspace colorspace;
177 struct vino_data_norm {
179 unsigned int width, height;
180 struct vino_clipping odd;
181 struct vino_clipping even;
184 unsigned int fps_min, fps_max;
188 struct vino_descriptor_table {
189 /* the number of PAGE_SIZE sized pages in the buffer */
190 unsigned int page_count;
191 /* virtual (kmalloc'd) pointers to the actual data
192 * (in PAGE_SIZE chunks, used with mmap streaming) */
193 unsigned long *virtual;
195 /* cpu address for the VINO descriptor table
196 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
197 unsigned long *dma_cpu;
198 /* dma address for the VINO descriptor table
199 * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
203 struct vino_framebuffer {
204 /* identifier nubmer */
206 /* the length of the whole buffer */
208 /* the length of actual data in buffer */
209 unsigned int data_size;
210 /* the data format */
211 unsigned int data_format;
212 /* the state of buffer data */
214 /* is the buffer mapped in user space? */
215 unsigned int map_count;
216 /* memory offset for mmap() */
219 unsigned int frame_counter;
220 /* timestamp (written when image capture finishes) */
221 struct timeval timestamp;
223 struct vino_descriptor_table desc_table;
225 spinlock_t state_lock;
228 struct vino_framebuffer_fifo {
235 unsigned int data[VINO_FRAMEBUFFER_MAX_COUNT];
238 struct vino_framebuffer_queue {
241 /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
245 /* data field of in and out contain index numbers for buffer */
246 struct vino_framebuffer_fifo in;
247 struct vino_framebuffer_fifo out;
249 struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_MAX_COUNT];
251 spinlock_t queue_lock;
252 struct semaphore queue_sem;
253 wait_queue_head_t frame_wait_queue;
256 struct vino_channel_settings {
257 unsigned int channel;
260 unsigned int data_format;
261 unsigned int data_norm;
262 struct vino_clipping clipping;
263 unsigned int decimation;
264 unsigned int line_size;
267 unsigned int framert_reg;
269 unsigned int fifo_threshold;
271 struct vino_framebuffer_queue fb_queue;
273 /* number of the current field */
276 /* read in progress */
278 /* streaming is active */
280 /* the driver is currently processing the queue */
283 struct semaphore sem;
284 spinlock_t capture_lock;
289 struct video_device *v4l_device;
293 /* the channel which owns this client:
294 * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
296 struct i2c_client *driver;
299 struct vino_settings {
300 struct vino_channel_settings a;
301 struct vino_channel_settings b;
303 struct vino_client decoder;
304 struct vino_client camera;
306 /* a lock for vino register access */
307 spinlock_t vino_lock;
308 /* a lock for channel input changes */
309 spinlock_t input_lock;
311 unsigned long dummy_page;
312 struct vino_descriptor_table dummy_desc_table;
315 /* Module parameters */
318 * Using vino_pixel_conversion the ARGB32-format pixels supplied
319 * by the VINO chip can be converted to more common formats
320 * like RGBA32 (or probably RGB24 in the future). This way we
321 * can give out data that can be specified correctly with
322 * the V4L2-definitions.
324 * The pixel format is specified as RGBA32 when no conversion
327 * Note that this only affects the 32-bit bit depth.
329 * Use non-zero value to enable conversion.
331 static int vino_pixel_conversion = 0;
332 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_v4l_device_name_a = "SGI VINO Channel A";
346 static const char *vino_v4l_device_name_b = "SGI VINO Channel B";
348 static const struct vino_input vino_inputs[] = {
351 .std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
354 .std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
356 .name = "D1 (IndyCam)",
357 .std = V4L2_STD_NTSC,
361 static const struct vino_data_format vino_data_formats[] = {
363 .description = "8-bit greyscale",
365 .pixelformat = V4L2_PIX_FMT_GREY,
366 .colorspace = V4L2_COLORSPACE_SMPTE170M,
368 .description = "8-bit dithered RGB 3-3-2",
370 .pixelformat = V4L2_PIX_FMT_RGB332,
371 .colorspace = V4L2_COLORSPACE_SRGB,
373 .description = "32-bit RGB",
375 .pixelformat = V4L2_PIX_FMT_RGB32,
376 .colorspace = V4L2_COLORSPACE_SRGB,
378 .description = "YUV 4:2:2",
380 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
381 .colorspace = V4L2_COLORSPACE_SMPTE170M,
383 .description = "24-bit RGB",
385 .pixelformat = V4L2_PIX_FMT_RGB24,
386 .colorspace = V4L2_COLORSPACE_SRGB,
390 static const struct vino_data_norm vino_data_norms[] = {
392 .description = "NTSC",
393 .std = V4L2_STD_NTSC,
397 .width = VINO_NTSC_WIDTH,
398 .height = VINO_NTSC_HEIGHT,
400 .top = VINO_CLIPPING_START_ODD_NTSC,
402 .bottom = VINO_CLIPPING_START_ODD_NTSC
403 + VINO_NTSC_HEIGHT / 2 - 1,
404 .right = VINO_NTSC_WIDTH,
407 .top = VINO_CLIPPING_START_EVEN_NTSC,
409 .bottom = VINO_CLIPPING_START_EVEN_NTSC
410 + VINO_NTSC_HEIGHT / 2 - 1,
411 .right = VINO_NTSC_WIDTH,
414 .description = "PAL",
419 .width = VINO_PAL_WIDTH,
420 .height = VINO_PAL_HEIGHT,
422 .top = VINO_CLIPPING_START_ODD_PAL,
424 .bottom = VINO_CLIPPING_START_ODD_PAL
425 + VINO_PAL_HEIGHT / 2 - 1,
426 .right = VINO_PAL_WIDTH,
429 .top = VINO_CLIPPING_START_EVEN_PAL,
431 .bottom = VINO_CLIPPING_START_EVEN_PAL
432 + VINO_PAL_HEIGHT / 2 - 1,
433 .right = VINO_PAL_WIDTH,
436 .description = "SECAM",
437 .std = V4L2_STD_SECAM,
441 .width = VINO_PAL_WIDTH,
442 .height = VINO_PAL_HEIGHT,
444 .top = VINO_CLIPPING_START_ODD_PAL,
446 .bottom = VINO_CLIPPING_START_ODD_PAL
447 + VINO_PAL_HEIGHT / 2 - 1,
448 .right = VINO_PAL_WIDTH,
451 .top = VINO_CLIPPING_START_EVEN_PAL,
453 .bottom = VINO_CLIPPING_START_EVEN_PAL
454 + VINO_PAL_HEIGHT / 2 - 1,
455 .right = VINO_PAL_WIDTH,
458 .description = "NTSC (D1 input)",
459 .std = V4L2_STD_NTSC,
463 .width = VINO_NTSC_WIDTH,
464 .height = VINO_NTSC_HEIGHT,
466 .top = VINO_CLIPPING_START_ODD_D1,
468 .bottom = VINO_CLIPPING_START_ODD_D1
469 + VINO_NTSC_HEIGHT / 2 - 1,
470 .right = VINO_NTSC_WIDTH,
473 .top = VINO_CLIPPING_START_EVEN_D1,
475 .bottom = VINO_CLIPPING_START_EVEN_D1
476 + VINO_NTSC_HEIGHT / 2 - 1,
477 .right = VINO_NTSC_WIDTH,
482 #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
484 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
486 .id = V4L2_CID_AUTOGAIN,
487 .type = V4L2_CTRL_TYPE_BOOLEAN,
488 .name = "Automatic Gain Control",
492 .default_value = INDYCAM_AGC_DEFAULT,
494 .reserved = { 0, 0 },
496 .id = V4L2_CID_AUTO_WHITE_BALANCE,
497 .type = V4L2_CTRL_TYPE_BOOLEAN,
498 .name = "Automatic White Balance",
502 .default_value = INDYCAM_AWB_DEFAULT,
504 .reserved = { 0, 0 },
507 .type = V4L2_CTRL_TYPE_INTEGER,
509 .minimum = INDYCAM_GAIN_MIN,
510 .maximum = INDYCAM_GAIN_MAX,
512 .default_value = INDYCAM_GAIN_DEFAULT,
514 .reserved = { 0, 0 },
516 .id = V4L2_CID_PRIVATE_BASE,
517 .type = V4L2_CTRL_TYPE_INTEGER,
518 .name = "Red Saturation",
519 .minimum = INDYCAM_RED_SATURATION_MIN,
520 .maximum = INDYCAM_RED_SATURATION_MAX,
522 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
524 .reserved = { 0, 0 },
526 .id = V4L2_CID_PRIVATE_BASE + 1,
527 .type = V4L2_CTRL_TYPE_INTEGER,
528 .name = "Blue Saturation",
529 .minimum = INDYCAM_BLUE_SATURATION_MIN,
530 .maximum = INDYCAM_BLUE_SATURATION_MAX,
532 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
534 .reserved = { 0, 0 },
536 .id = V4L2_CID_RED_BALANCE,
537 .type = V4L2_CTRL_TYPE_INTEGER,
538 .name = "Red Balance",
539 .minimum = INDYCAM_RED_BALANCE_MIN,
540 .maximum = INDYCAM_RED_BALANCE_MAX,
542 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
544 .reserved = { 0, 0 },
546 .id = V4L2_CID_BLUE_BALANCE,
547 .type = V4L2_CTRL_TYPE_INTEGER,
548 .name = "Blue Balance",
549 .minimum = INDYCAM_BLUE_BALANCE_MIN,
550 .maximum = INDYCAM_BLUE_BALANCE_MAX,
552 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
554 .reserved = { 0, 0 },
556 .id = V4L2_CID_EXPOSURE,
557 .type = V4L2_CTRL_TYPE_INTEGER,
558 .name = "Shutter Control",
559 .minimum = INDYCAM_SHUTTER_MIN,
560 .maximum = INDYCAM_SHUTTER_MAX,
562 .default_value = INDYCAM_SHUTTER_DEFAULT,
564 .reserved = { 0, 0 },
566 .id = V4L2_CID_GAMMA,
567 .type = V4L2_CTRL_TYPE_INTEGER,
569 .minimum = INDYCAM_GAMMA_MIN,
570 .maximum = INDYCAM_GAMMA_MAX,
572 .default_value = INDYCAM_GAMMA_DEFAULT,
574 .reserved = { 0, 0 },
578 #define VINO_SAA7191_V4L2_CONTROL_COUNT 2
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 .reserved = { 0, 0 },
592 .id = V4L2_CID_PRIVATE_BASE,
593 .type = V4L2_CTRL_TYPE_BOOLEAN,
594 .name = "VTR Time Constant",
595 .minimum = SAA7191_VTRC_MIN,
596 .maximum = SAA7191_VTRC_MAX,
598 .default_value = SAA7191_VTRC_DEFAULT,
600 .reserved = { 0, 0 },
604 /* VINO I2C bus functions */
606 unsigned i2c_vino_getctrl(void *data)
608 return vino->i2c_control;
611 void i2c_vino_setctrl(void *data, unsigned val)
613 vino->i2c_control = val;
616 unsigned i2c_vino_rdata(void *data)
618 return vino->i2c_data;
621 void i2c_vino_wdata(void *data, unsigned val)
623 vino->i2c_data = val;
626 static struct i2c_algo_sgi_data i2c_sgi_vino_data =
628 .getctrl = &i2c_vino_getctrl,
629 .setctrl = &i2c_vino_setctrl,
630 .rdata = &i2c_vino_rdata,
631 .wdata = &i2c_vino_wdata,
637 * There are two possible clients on VINO I2C bus, so we limit usage only
640 static int i2c_vino_client_reg(struct i2c_client *client)
644 spin_lock(&vino_drvdata->input_lock);
645 switch (client->driver->id) {
646 case I2C_DRIVERID_SAA7191:
647 if (vino_drvdata->decoder.driver)
650 vino_drvdata->decoder.driver = client;
652 case I2C_DRIVERID_INDYCAM:
653 if (vino_drvdata->camera.driver)
656 vino_drvdata->camera.driver = client;
661 spin_unlock(&vino_drvdata->input_lock);
666 static int i2c_vino_client_unreg(struct i2c_client *client)
670 spin_lock(&vino_drvdata->input_lock);
671 if (client == vino_drvdata->decoder.driver) {
672 if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
675 vino_drvdata->decoder.driver = NULL;
676 } else if (client == vino_drvdata->camera.driver) {
677 if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
680 vino_drvdata->camera.driver = NULL;
682 spin_unlock(&vino_drvdata->input_lock);
687 static struct i2c_adapter vino_i2c_adapter =
689 .name = "VINO I2C bus",
690 .id = I2C_HW_SGI_VINO,
691 .algo_data = &i2c_sgi_vino_data,
692 .client_register = &i2c_vino_client_reg,
693 .client_unregister = &i2c_vino_client_unreg,
696 static int vino_i2c_add_bus(void)
698 return i2c_sgi_add_bus(&vino_i2c_adapter);
701 static int vino_i2c_del_bus(void)
703 return i2c_sgi_del_bus(&vino_i2c_adapter);
706 static int i2c_camera_command(unsigned int cmd, void *arg)
708 return vino_drvdata->camera.driver->
709 driver->command(vino_drvdata->camera.driver,
713 static int i2c_decoder_command(unsigned int cmd, void *arg)
715 return vino_drvdata->decoder.driver->
716 driver->command(vino_drvdata->decoder.driver,
720 /* VINO framebuffer/DMA descriptor management */
722 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
727 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
729 for (i = 0; i < count; i++) {
730 mem_map_unreserve(virt_to_page(fb->desc_table.virtual[i]));
731 dma_unmap_single(NULL,
732 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
733 PAGE_SIZE, DMA_FROM_DEVICE);
734 free_page(fb->desc_table.virtual[i]);
737 dma_free_coherent(NULL,
738 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
739 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
741 kfree(fb->desc_table.virtual);
743 memset(fb, 0, sizeof(struct vino_framebuffer));
746 static void vino_free_buffer(struct vino_framebuffer *fb)
748 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
751 static int vino_allocate_buffer(struct vino_framebuffer *fb,
754 unsigned int count, i, j;
757 dprintk("vino_allocate_buffer():\n");
762 memset(fb, 0, sizeof(struct vino_framebuffer));
764 count = ((size / PAGE_SIZE) + 4) & ~3;
766 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
769 /* allocate memory for table with virtual (page) addresses */
770 fb->desc_table.virtual = (unsigned long *)
771 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
772 if (!fb->desc_table.virtual)
775 /* allocate memory for table with dma addresses
776 * (has space for four extra descriptors) */
777 fb->desc_table.dma_cpu =
778 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
779 sizeof(dma_addr_t), &fb->desc_table.dma,
780 GFP_KERNEL | GFP_DMA);
781 if (!fb->desc_table.dma_cpu) {
783 goto out_free_virtual;
786 /* allocate pages for the buffer and acquire the according
788 for (i = 0; i < count; i++) {
789 dma_addr_t dma_data_addr;
791 fb->desc_table.virtual[i] =
792 get_zeroed_page(GFP_KERNEL | GFP_DMA);
793 if (!fb->desc_table.virtual[i]) {
800 (void *)fb->desc_table.virtual[i],
801 PAGE_SIZE, DMA_FROM_DEVICE);
803 for (j = 0; j < VINO_PAGE_RATIO; j++) {
804 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
805 dma_data_addr + VINO_PAGE_SIZE * j;
808 mem_map_reserve(virt_to_page(fb->desc_table.virtual[i]));
811 /* page_count needs to be set anyway, because the descriptor table has
812 * been allocated according to this number */
813 fb->desc_table.page_count = count;
816 /* the descriptor with index i doesn't contain
817 * a valid address yet */
818 vino_free_buffer_with_count(fb, i);
823 fb->size = count * PAGE_SIZE;
824 fb->data_format = VINO_DATA_FMT_NONE;
826 /* set the dma stop-bit for the last (count+1)th descriptor */
827 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
831 kfree(fb->desc_table.virtual);
836 /* user buffers not fully implemented yet */
837 static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
841 unsigned int count, i, j;
844 dprintk("vino_prepare_user_buffer():\n");
849 memset(fb, 0, sizeof(struct vino_framebuffer));
851 count = ((size / PAGE_SIZE)) & ~3;
853 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
856 /* allocate memory for table with virtual (page) addresses */
857 fb->desc_table.virtual = (unsigned long *)
858 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
859 if (!fb->desc_table.virtual)
862 /* allocate memory for table with dma addresses
863 * (has space for four extra descriptors) */
864 fb->desc_table.dma_cpu =
865 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
866 sizeof(dma_addr_t), &fb->desc_table.dma,
867 GFP_KERNEL | GFP_DMA);
868 if (!fb->desc_table.dma_cpu) {
870 goto out_free_virtual;
873 /* allocate pages for the buffer and acquire the according
875 for (i = 0; i < count; i++) {
876 dma_addr_t dma_data_addr;
878 fb->desc_table.virtual[i] =
879 get_zeroed_page(GFP_KERNEL | GFP_DMA);
880 if (!fb->desc_table.virtual[i]) {
887 (void *)fb->desc_table.virtual[i],
888 PAGE_SIZE, DMA_FROM_DEVICE);
890 for (j = 0; j < VINO_PAGE_RATIO; j++) {
891 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
892 dma_data_addr + VINO_PAGE_SIZE * j;
895 mem_map_reserve(virt_to_page(fb->desc_table.virtual[i]));
898 /* page_count needs to be set anyway, because the descriptor table has
899 * been allocated according to this number */
900 fb->desc_table.page_count = count;
903 /* the descriptor with index i doesn't contain
904 * a valid address yet */
905 vino_free_buffer_with_count(fb, i);
910 fb->size = count * PAGE_SIZE;
912 /* set the dma stop-bit for the last (count+1)th descriptor */
913 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
917 kfree(fb->desc_table.virtual);
922 static void vino_sync_buffer(struct vino_framebuffer *fb)
926 dprintk("vino_sync_buffer():\n");
928 for (i = 0; i < fb->desc_table.page_count; i++)
929 dma_sync_single(NULL,
930 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
931 PAGE_SIZE, DMA_FROM_DEVICE);
934 /* Framebuffer fifo functions (need to be locked externally) */
936 static void vino_fifo_init(struct vino_framebuffer_fifo *f,
944 if (length > VINO_FRAMEBUFFER_MAX_COUNT)
945 length = VINO_FRAMEBUFFER_MAX_COUNT;
950 /* returns true/false */
951 static int vino_fifo_has_id(struct vino_framebuffer_fifo *f, unsigned int id)
954 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
955 if (f->data[i] == id)
962 /* returns true/false */
963 static int vino_fifo_full(struct vino_framebuffer_fifo *f)
965 return (f->used == f->length);
968 static unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
973 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
975 if (id >= f->length) {
976 return VINO_QUEUE_ERROR;
979 if (vino_fifo_has_id(f, id)) {
980 return VINO_QUEUE_ERROR;
983 if (f->used < f->length) {
984 f->data[f->tail] = id;
985 f->tail = (f->tail + 1) % f->length;
988 return VINO_QUEUE_ERROR;
994 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
997 *id = f->data[f->head];
999 return VINO_QUEUE_ERROR;
1005 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
1008 *id = f->data[f->head];
1009 f->head = (f->head + 1) % f->length;
1012 return VINO_QUEUE_ERROR;
1018 /* Framebuffer queue functions */
1020 /* execute with queue_lock locked */
1021 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
1022 unsigned int length)
1027 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
1028 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
1029 for (i = 0; i < length; i++) {
1030 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
1032 vino_free_buffer(q->buffer[i]);
1033 kfree(q->buffer[i]);
1036 q->type = VINO_MEMORY_NONE;
1040 static void vino_queue_free(struct vino_framebuffer_queue *q)
1042 dprintk("vino_queue_free():\n");
1044 if (q->magic != VINO_QUEUE_MAGIC)
1046 if (q->type != VINO_MEMORY_MMAP)
1049 down(&q->queue_sem);
1051 vino_queue_free_with_count(q, q->length);
1056 static int vino_queue_init(struct vino_framebuffer_queue *q,
1057 unsigned int *length)
1062 dprintk("vino_queue_init(): length = %d\n", *length);
1064 if (q->magic == VINO_QUEUE_MAGIC) {
1065 dprintk("vino_queue_init(): queue already initialized!\n");
1069 if (q->type != VINO_MEMORY_NONE) {
1070 dprintk("vino_queue_init(): queue already initialized!\n");
1077 down(&q->queue_sem);
1079 if (*length > VINO_FRAMEBUFFER_MAX_COUNT)
1080 *length = VINO_FRAMEBUFFER_MAX_COUNT;
1084 for (i = 0; i < *length; i++) {
1085 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1086 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1088 if (!q->buffer[i]) {
1089 dprintk("vino_queue_init(): kmalloc() failed\n");
1094 ret = vino_allocate_buffer(q->buffer[i],
1095 VINO_FRAMEBUFFER_SIZE);
1097 kfree(q->buffer[i]);
1098 dprintk("vino_queue_init(): "
1099 "vino_allocate_buffer() failed\n");
1103 q->buffer[i]->id = i;
1105 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1106 q->buffer[i - 1]->size;
1108 q->buffer[i]->offset = 0;
1111 spin_lock_init(&q->buffer[i]->state_lock);
1113 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1114 "size = %d\n", i, q->buffer[i]->offset,
1115 q->buffer[i]->size);
1119 vino_queue_free_with_count(q, i);
1122 q->length = *length;
1123 vino_fifo_init(&q->in, q->length);
1124 vino_fifo_init(&q->out, q->length);
1125 q->type = VINO_MEMORY_MMAP;
1126 q->magic = VINO_QUEUE_MAGIC;
1134 static struct vino_framebuffer *vino_queue_add(struct
1135 vino_framebuffer_queue *q,
1138 struct vino_framebuffer *ret = NULL;
1140 unsigned long flags;
1142 dprintk("vino_queue_add(): id = %d\n", id);
1144 if (q->magic != VINO_QUEUE_MAGIC) {
1148 spin_lock_irqsave(&q->queue_lock, flags);
1153 if (id >= q->length)
1156 /* not needed?: if (vino_fifo_full(&q->out)) {
1159 /* check that outgoing queue isn't already full
1160 * (or that it won't become full) */
1161 total = vino_fifo_get_used(&q->in) +
1162 vino_fifo_get_used(&q->out);
1163 if (total >= q->length)
1166 if (vino_fifo_enqueue(&q->in, id))
1169 ret = q->buffer[id];
1172 spin_unlock_irqrestore(&q->queue_lock, flags);
1177 static struct vino_framebuffer *vino_queue_transfer(struct
1178 vino_framebuffer_queue *q)
1180 struct vino_framebuffer *ret = NULL;
1181 struct vino_framebuffer *fb;
1183 unsigned long flags;
1185 dprintk("vino_queue_transfer():\n");
1187 if (q->magic != VINO_QUEUE_MAGIC) {
1191 spin_lock_irqsave(&q->queue_lock, flags);
1196 // now this actually removes an entry from the incoming queue
1197 if (vino_fifo_dequeue(&q->in, &id)) {
1201 dprintk("vino_queue_transfer(): id = %d\n", id);
1204 // we have already checked that the outgoing queue is not full, but...
1205 if (vino_fifo_enqueue(&q->out, id)) {
1206 printk(KERN_ERR "vino_queue_transfer(): "
1207 "outgoing queue is full, this shouldn't happen!\n");
1213 spin_unlock_irqrestore(&q->queue_lock, flags);
1218 /* returns true/false */
1219 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1223 unsigned long flags;
1225 if (q->magic != VINO_QUEUE_MAGIC) {
1229 spin_lock_irqsave(&q->queue_lock, flags);
1234 ret = vino_fifo_has_id(&q->in, id);
1237 spin_unlock_irqrestore(&q->queue_lock, flags);
1242 /* returns true/false */
1243 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1247 unsigned long flags;
1249 if (q->magic != VINO_QUEUE_MAGIC) {
1253 spin_lock_irqsave(&q->queue_lock, flags);
1258 ret = vino_fifo_has_id(&q->out, id);
1261 spin_unlock_irqrestore(&q->queue_lock, flags);
1266 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1270 unsigned long flags;
1272 if (q->magic != VINO_QUEUE_MAGIC) {
1273 return VINO_QUEUE_ERROR;
1276 spin_lock_irqsave(&q->queue_lock, flags);
1278 if (q->length == 0) {
1279 ret = VINO_QUEUE_ERROR;
1283 *used = vino_fifo_get_used(&q->in);
1286 spin_unlock_irqrestore(&q->queue_lock, flags);
1291 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1295 unsigned long flags;
1297 if (q->magic != VINO_QUEUE_MAGIC) {
1298 return VINO_QUEUE_ERROR;
1301 spin_lock_irqsave(&q->queue_lock, flags);
1303 if (q->length == 0) {
1304 ret = VINO_QUEUE_ERROR;
1308 *used = vino_fifo_get_used(&q->out);
1311 spin_unlock_irqrestore(&q->queue_lock, flags);
1316 static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1317 unsigned int *total)
1320 unsigned long flags;
1322 if (q->magic != VINO_QUEUE_MAGIC) {
1323 return VINO_QUEUE_ERROR;
1326 spin_lock_irqsave(&q->queue_lock, flags);
1328 if (q->length == 0) {
1329 ret = VINO_QUEUE_ERROR;
1333 *total = vino_fifo_get_used(&q->in) +
1334 vino_fifo_get_used(&q->out);
1337 spin_unlock_irqrestore(&q->queue_lock, flags);
1342 static struct vino_framebuffer *vino_queue_peek(struct
1343 vino_framebuffer_queue *q,
1346 struct vino_framebuffer *ret = NULL;
1347 unsigned long flags;
1349 if (q->magic != VINO_QUEUE_MAGIC) {
1353 spin_lock_irqsave(&q->queue_lock, flags);
1358 if (vino_fifo_peek(&q->in, id)) {
1362 ret = q->buffer[*id];
1364 spin_unlock_irqrestore(&q->queue_lock, flags);
1369 static struct vino_framebuffer *vino_queue_remove(struct
1370 vino_framebuffer_queue *q,
1373 struct vino_framebuffer *ret = NULL;
1374 unsigned long flags;
1375 dprintk("vino_queue_remove():\n");
1377 if (q->magic != VINO_QUEUE_MAGIC) {
1381 spin_lock_irqsave(&q->queue_lock, flags);
1386 if (vino_fifo_dequeue(&q->out, id)) {
1390 dprintk("vino_queue_remove(): id = %d\n", *id);
1391 ret = q->buffer[*id];
1393 spin_unlock_irqrestore(&q->queue_lock, flags);
1399 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1402 struct vino_framebuffer *ret = NULL;
1403 unsigned long flags;
1405 if (q->magic != VINO_QUEUE_MAGIC) {
1409 spin_lock_irqsave(&q->queue_lock, flags);
1414 if (id >= q->length)
1417 ret = q->buffer[id];
1419 spin_unlock_irqrestore(&q->queue_lock, flags);
1424 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1426 unsigned int length = 0;
1427 unsigned long flags;
1429 if (q->magic != VINO_QUEUE_MAGIC) {
1433 spin_lock_irqsave(&q->queue_lock, flags);
1435 spin_unlock_irqrestore(&q->queue_lock, flags);
1440 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1444 unsigned long flags;
1446 if (q->magic != VINO_QUEUE_MAGIC) {
1450 spin_lock_irqsave(&q->queue_lock, flags);
1451 for (i = 0; i < q->length; i++) {
1452 if (q->buffer[i]->map_count > 0) {
1457 spin_unlock_irqrestore(&q->queue_lock, flags);
1462 /* VINO functions */
1464 /* execute with input_lock locked */
1465 static void vino_update_line_size(struct vino_channel_settings *vcs)
1467 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1468 unsigned int d = vcs->decimation;
1469 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1472 dprintk("update_line_size(): before: w = %d, d = %d, "
1473 "line_size = %d\n", w, d, vcs->line_size);
1474 /* line size must be multiple of 8 bytes */
1475 lsize = (bpp * (w / d)) & ~7;
1476 w = (lsize / bpp) * d;
1478 vcs->clipping.right = vcs->clipping.left + w;
1479 vcs->line_size = lsize;
1480 dprintk("update_line_size(): after: w = %d, d = %d, "
1481 "line_size = %d\n", w, d, vcs->line_size);
1484 /* execute with input_lock locked */
1485 static void vino_set_clipping(struct vino_channel_settings *vcs,
1486 unsigned int x, unsigned int y,
1487 unsigned int w, unsigned int h)
1489 unsigned int maxwidth, maxheight;
1492 maxwidth = vino_data_norms[vcs->data_norm].width;
1493 maxheight = vino_data_norms[vcs->data_norm].height;
1494 d = vcs->decimation;
1496 y &= ~1; /* odd/even fields */
1501 if (y > maxheight) {
1505 if (((w / d) < VINO_MIN_WIDTH)
1506 || ((h / d) < VINO_MIN_HEIGHT)) {
1507 w = VINO_MIN_WIDTH * d;
1508 h = VINO_MIN_HEIGHT * d;
1511 if ((x + w) > maxwidth) {
1513 if ((w / d) < VINO_MIN_WIDTH)
1514 x = maxwidth - VINO_MIN_WIDTH * d;
1516 if ((y + h) > maxheight) {
1518 if ((h / d) < VINO_MIN_HEIGHT)
1519 y = maxheight - VINO_MIN_HEIGHT * d;
1522 vcs->clipping.left = x;
1523 vcs->clipping.top = y;
1524 vcs->clipping.right = x + w;
1525 vcs->clipping.bottom = y + h;
1527 vino_update_line_size(vcs);
1529 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1530 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1531 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1534 /* execute with input_lock locked */
1535 static void vino_set_default_clipping(struct vino_channel_settings *vcs)
1537 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1538 vino_data_norms[vcs->data_norm].height);
1541 /* execute with input_lock locked */
1542 static void vino_set_scaling(struct vino_channel_settings *vcs,
1543 unsigned int w, unsigned int h)
1545 unsigned int x, y, curw, curh, d;
1547 x = vcs->clipping.left;
1548 y = vcs->clipping.top;
1549 curw = vcs->clipping.right - vcs->clipping.left;
1550 curh = vcs->clipping.bottom - vcs->clipping.top;
1552 d = max(curw / w, curh / h);
1554 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1555 w, h, curw, curh, d);
1564 vcs->decimation = d;
1565 vino_set_clipping(vcs, x, y, w * d, h * d);
1567 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1568 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1569 vcs->decimation, vcs->line_size);
1572 /* execute with input_lock locked */
1573 static void vino_reset_scaling(struct vino_channel_settings *vcs)
1575 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1576 vcs->clipping.bottom - vcs->clipping.top);
1579 /* execute with input_lock locked */
1580 static void vino_set_framerate(struct vino_channel_settings *vcs,
1585 switch (vcs->data_norm) {
1586 case VINO_DATA_NORM_NTSC:
1587 case VINO_DATA_NORM_D1:
1588 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1590 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1591 fps = vino_data_norms[vcs->data_norm].fps_min;
1592 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1593 fps = vino_data_norms[vcs->data_norm].fps_max;
1612 mask = VINO_FRAMERT_FULL;
1614 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1616 case VINO_DATA_NORM_PAL:
1617 case VINO_DATA_NORM_SECAM:
1618 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1620 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1621 fps = vino_data_norms[vcs->data_norm].fps_min;
1622 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1623 fps = vino_data_norms[vcs->data_norm].fps_max;
1642 mask = VINO_FRAMERT_FULL;
1644 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1651 /* execute with input_lock locked */
1652 static void vino_set_default_framerate(struct vino_channel_settings *vcs)
1654 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1658 * Prepare VINO for DMA transfer...
1659 * (execute only with vino_lock and input_lock locked)
1661 static int vino_dma_setup(struct vino_channel_settings *vcs,
1662 struct vino_framebuffer *fb)
1665 struct sgi_vino_channel *ch;
1666 const struct vino_data_norm *norm;
1668 dprintk("vino_dma_setup():\n");
1671 fb->frame_counter = 0;
1673 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1674 norm = &vino_data_norms[vcs->data_norm];
1679 /* VINO line size register is set 8 bytes less than actual */
1680 ch->line_size = vcs->line_size - 8;
1682 /* let VINO know where to transfer data */
1683 ch->start_desc_tbl = fb->desc_table.dma;
1684 ch->next_4_desc = fb->desc_table.dma;
1686 /* give vino time to fetch the first four descriptors, 5 usec
1687 * should be more than enough time */
1688 udelay(VINO_DESC_FETCH_DELAY);
1690 /* set the alpha register */
1691 ch->alpha = vcs->alpha;
1693 /* set clipping registers */
1694 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1695 VINO_CLIP_EVEN(norm->even.top +
1696 vcs->clipping.top / 2) |
1697 VINO_CLIP_X(vcs->clipping.left);
1698 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1699 vcs->clipping.bottom / 2 - 1) |
1700 VINO_CLIP_EVEN(norm->even.top +
1701 vcs->clipping.bottom / 2 - 1) |
1702 VINO_CLIP_X(vcs->clipping.right);
1703 /* FIXME: end-of-field bug workaround
1704 VINO_CLIP_X(VINO_PAL_WIDTH);
1707 /* set the size of actual content in the buffer (DECIMATION !) */
1708 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1710 ((vcs->clipping.bottom - vcs->clipping.top) /
1712 vino_data_formats[vcs->data_format].bpp;
1714 ch->frame_rate = vcs->framert_reg;
1716 ctrl = vino->control;
1717 intr = vino->intr_status;
1719 if (vcs->channel == VINO_CHANNEL_A) {
1720 /* All interrupt conditions for this channel was cleared
1721 * so clear the interrupt status register and enable
1723 intr &= ~VINO_INTSTAT_A;
1724 ctrl |= VINO_CTRL_A_INT;
1726 /* enable synchronization */
1727 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1729 /* enable frame assembly */
1730 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1732 /* set decimation used */
1733 if (vcs->decimation < 2)
1734 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1736 ctrl |= VINO_CTRL_A_DEC_ENBL;
1737 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1738 ctrl |= (vcs->decimation - 1) <<
1739 VINO_CTRL_A_DEC_SCALE_SHIFT;
1742 /* select input interface */
1743 if (vcs->input == VINO_INPUT_D1)
1744 ctrl |= VINO_CTRL_A_SELECT;
1746 ctrl &= ~VINO_CTRL_A_SELECT;
1749 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1750 VINO_CTRL_A_DITHER);
1752 intr &= ~VINO_INTSTAT_B;
1753 ctrl |= VINO_CTRL_B_INT;
1755 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1756 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1758 if (vcs->decimation < 2)
1759 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1761 ctrl |= VINO_CTRL_B_DEC_ENBL;
1762 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1763 ctrl |= (vcs->decimation - 1) <<
1764 VINO_CTRL_B_DEC_SCALE_SHIFT;
1767 if (vcs->input == VINO_INPUT_D1)
1768 ctrl |= VINO_CTRL_B_SELECT;
1770 ctrl &= ~VINO_CTRL_B_SELECT;
1772 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1773 VINO_CTRL_B_DITHER);
1777 fb->data_format = vcs->data_format;
1779 switch (vcs->data_format) {
1780 case VINO_DATA_FMT_GREY:
1781 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1782 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1784 case VINO_DATA_FMT_RGB32:
1785 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1786 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1788 case VINO_DATA_FMT_YUV:
1789 /* nothing needs to be done */
1791 case VINO_DATA_FMT_RGB332:
1792 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1793 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1794 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1798 vino->intr_status = intr;
1799 vino->control = ctrl;
1804 /* (execute only with vino_lock locked) */
1805 static void vino_dma_start(struct vino_channel_settings *vcs)
1807 u32 ctrl = vino->control;
1809 dprintk("vino_dma_start():\n");
1810 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1811 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1812 vino->control = ctrl;
1815 /* (execute only with vino_lock locked) */
1816 static void vino_dma_stop(struct vino_channel_settings *vcs)
1818 u32 ctrl = vino->control;
1820 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1821 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1822 vino->control = ctrl;
1823 dprintk("vino_dma_stop():\n");
1827 * Load dummy page to descriptor registers. This prevents generating of
1828 * spurious interrupts. (execute only with vino_lock locked)
1830 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1832 struct sgi_vino_channel *ch;
1834 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1839 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1840 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1842 udelay(VINO_DESC_FETCH_DELAY);
1843 dprintk("channel %c clear interrupt condition\n",
1844 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1847 static int vino_capture(struct vino_channel_settings *vcs,
1848 struct vino_framebuffer *fb)
1851 unsigned long flags, flags2;
1853 spin_lock_irqsave(&fb->state_lock, flags);
1855 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1857 fb->state = VINO_FRAMEBUFFER_IN_USE;
1859 spin_unlock_irqrestore(&fb->state_lock, flags);
1864 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1865 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1867 vino_dma_setup(vcs, fb);
1868 vino_dma_start(vcs);
1870 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1871 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1877 struct vino_framebuffer *vino_capture_enqueue(struct
1878 vino_channel_settings *vcs,
1881 struct vino_framebuffer *fb;
1882 unsigned long flags;
1884 dprintk("vino_capture_enqueue():\n");
1886 spin_lock_irqsave(&vcs->capture_lock, flags);
1888 fb = vino_queue_add(&vcs->fb_queue, index);
1890 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1895 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1900 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1902 struct vino_framebuffer *fb;
1903 unsigned int incoming, id;
1905 unsigned long flags, flags2;
1907 dprintk("vino_capture_next():\n");
1909 spin_lock_irqsave(&vcs->capture_lock, flags);
1912 /* start capture only if capture isn't in progress already */
1913 if (vcs->capturing) {
1914 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1919 /* capture next frame:
1920 * stop capture if capturing is not set */
1921 if (!vcs->capturing) {
1922 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1927 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
1929 dprintk("vino_capture_next(): vino_queue_get_incoming() "
1934 if (incoming == 0) {
1935 dprintk("vino_capture_next(): no buffers available\n");
1939 fb = vino_queue_peek(&vcs->fb_queue, &id);
1941 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
1946 spin_lock_irqsave(&fb->state_lock, flags2);
1947 fb->state = VINO_FRAMEBUFFER_UNUSED;
1948 spin_unlock_irqrestore(&fb->state_lock, flags2);
1954 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1956 err = vino_capture(vcs, fb);
1962 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1967 static int vino_is_capturing(struct vino_channel_settings *vcs)
1970 unsigned long flags;
1972 spin_lock_irqsave(&vcs->capture_lock, flags);
1974 ret = vcs->capturing;
1976 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1981 /* waits until a frame is captured */
1982 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
1987 dprintk("vino_wait_for_frame():\n");
1989 init_waitqueue_entry(&wait, current);
1990 /* add ourselves into wait queue */
1991 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
1992 /* and set current state */
1993 set_current_state(TASK_INTERRUPTIBLE);
1995 /* to ensure that schedule_timeout will return immediately
1996 * if VINO interrupt was triggred meanwhile */
1997 schedule_timeout(HZ / 10);
1999 if (signal_pending(current))
2002 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2004 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2005 err ? "failed" : "ok");
2010 /* the function assumes that PAGE_SIZE % 4 == 0 */
2011 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2012 unsigned char *pageptr;
2013 unsigned int page, i;
2016 for (page = 0; page < fb->desc_table.page_count; page++) {
2017 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2019 for (i = 0; i < PAGE_SIZE; i += 4) {
2021 pageptr[0] = pageptr[3];
2022 pageptr[1] = pageptr[2];
2023 pageptr[2] = pageptr[1];
2030 /* checks if the buffer is in correct state and syncs data */
2031 static int vino_check_buffer(struct vino_channel_settings *vcs,
2032 struct vino_framebuffer *fb)
2035 unsigned long flags;
2037 dprintk("vino_check_buffer():\n");
2039 spin_lock_irqsave(&fb->state_lock, flags);
2040 switch (fb->state) {
2041 case VINO_FRAMEBUFFER_IN_USE:
2044 case VINO_FRAMEBUFFER_READY:
2045 vino_sync_buffer(fb);
2046 fb->state = VINO_FRAMEBUFFER_UNUSED;
2051 spin_unlock_irqrestore(&fb->state_lock, flags);
2054 if (vino_pixel_conversion
2055 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2056 vino_convert_to_rgba(fb);
2058 } else if (err && (err != -EINVAL)) {
2059 dprintk("vino_check_buffer(): buffer not ready\n");
2061 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2063 vino_clear_interrupt(vcs);
2064 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2070 /* forcefully terminates capture */
2071 static void vino_capture_stop(struct vino_channel_settings *vcs)
2073 unsigned int incoming = 0, outgoing = 0, id;
2074 unsigned long flags, flags2;
2076 dprintk("vino_capture_stop():\n");
2078 spin_lock_irqsave(&vcs->capture_lock, flags);
2079 /* unset capturing to stop queue processing */
2082 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2085 vino_clear_interrupt(vcs);
2087 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2089 /* remove all items from the queue */
2090 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2091 dprintk("vino_capture_stop(): "
2092 "vino_queue_get_incoming() failed\n");
2095 while (incoming > 0) {
2096 vino_queue_transfer(&vcs->fb_queue);
2098 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2099 dprintk("vino_capture_stop(): "
2100 "vino_queue_get_incoming() failed\n");
2105 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2106 dprintk("vino_capture_stop(): "
2107 "vino_queue_get_outgoing() failed\n");
2110 while (outgoing > 0) {
2111 vino_queue_remove(&vcs->fb_queue, &id);
2113 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2114 dprintk("vino_capture_stop(): "
2115 "vino_queue_get_outgoing() failed\n");
2121 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2124 static int vino_capture_failed(struct vino_channel_settings *vcs)
2126 struct vino_framebuffer *fb;
2127 unsigned long flags;
2131 dprintk("vino_capture_failed():\n");
2133 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2136 vino_clear_interrupt(vcs);
2138 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2140 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2141 if (ret == VINO_QUEUE_ERROR) {
2142 dprintk("vino_queue_get_incoming() failed\n");
2146 /* no buffers to process */
2150 fb = vino_queue_peek(&vcs->fb_queue, &i);
2152 dprintk("vino_queue_peek() failed\n");
2156 spin_lock_irqsave(&fb->state_lock, flags);
2157 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2158 fb->state = VINO_FRAMEBUFFER_UNUSED;
2159 vino_queue_transfer(&vcs->fb_queue);
2160 vino_queue_remove(&vcs->fb_queue, &i);
2161 /* we should actually discard the newest frame,
2162 * but who cares ... */
2164 spin_unlock_irqrestore(&fb->state_lock, flags);
2169 static void vino_frame_done(struct vino_channel_settings *vcs,
2172 struct vino_framebuffer *fb;
2173 unsigned long flags;
2175 spin_lock_irqsave(&vcs->capture_lock, flags);
2176 fb = vino_queue_transfer(&vcs->fb_queue);
2178 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2179 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2182 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2184 fb->frame_counter = fc;
2185 do_gettimeofday(&fb->timestamp);
2187 spin_lock_irqsave(&fb->state_lock, flags);
2188 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2189 fb->state = VINO_FRAMEBUFFER_READY;
2190 spin_unlock_irqrestore(&fb->state_lock, flags);
2192 wake_up(&vcs->fb_queue.frame_wait_queue);
2194 vino_capture_next(vcs, 0);
2197 static irqreturn_t vino_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2200 unsigned int fc_a, fc_b;
2204 spin_lock(&vino_drvdata->vino_lock);
2206 intr = vino->intr_status;
2207 fc_a = vino->a.field_counter / 2;
2208 fc_b = vino->b.field_counter / 2;
2210 // TODO: handle error-interrupts in some special way ?
2212 if (intr & VINO_INTSTAT_A) {
2213 if (intr & VINO_INTSTAT_A_EOF) {
2214 vino_drvdata->a.field++;
2215 if (vino_drvdata->a.field > 1) {
2216 vino_dma_stop(&vino_drvdata->a);
2217 vino_clear_interrupt(&vino_drvdata->a);
2218 vino_drvdata->a.field = 0;
2221 dprintk("intr: channel A end-of-field interrupt: "
2224 vino_dma_stop(&vino_drvdata->a);
2225 vino_clear_interrupt(&vino_drvdata->a);
2227 dprintk("channel A error interrupt: %04x\n", intr);
2230 if (intr & VINO_INTSTAT_B) {
2231 if (intr & VINO_INTSTAT_B_EOF) {
2232 vino_drvdata->b.field++;
2233 if (vino_drvdata->b.field > 1) {
2234 vino_dma_stop(&vino_drvdata->b);
2235 vino_clear_interrupt(&vino_drvdata->b);
2236 vino_drvdata->b.field = 0;
2239 dprintk("intr: channel B end-of-field interrupt: "
2242 vino_dma_stop(&vino_drvdata->b);
2243 vino_clear_interrupt(&vino_drvdata->b);
2245 dprintk("channel B error interrupt: %04x\n", intr);
2249 /* always remember to clear interrupt status */
2250 vino->intr_status = ~intr;
2252 spin_unlock(&vino_drvdata->vino_lock);
2255 vino_frame_done(&vino_drvdata->a, fc_a);
2256 dprintk("channel A frame done, interrupt: %d\n", intr);
2259 vino_frame_done(&vino_drvdata->b, fc_b);
2260 dprintk("channel B frame done, interrupt: %d\n", intr);
2266 /* VINO video input management */
2268 static int vino_get_saa7191_input(int input)
2271 case VINO_INPUT_COMPOSITE:
2272 return SAA7191_INPUT_COMPOSITE;
2273 case VINO_INPUT_SVIDEO:
2274 return SAA7191_INPUT_SVIDEO;
2276 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2277 "invalid input!\n");
2282 static int vino_get_saa7191_norm(int norm)
2285 case VINO_DATA_NORM_AUTO:
2286 return SAA7191_NORM_AUTO;
2287 case VINO_DATA_NORM_PAL:
2288 return SAA7191_NORM_PAL;
2289 case VINO_DATA_NORM_NTSC:
2290 return SAA7191_NORM_NTSC;
2291 case VINO_DATA_NORM_SECAM:
2292 return SAA7191_NORM_SECAM;
2294 printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
2300 /* execute with input_lock locked */
2301 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2303 switch(vcs->input) {
2304 case VINO_INPUT_COMPOSITE:
2305 case VINO_INPUT_SVIDEO:
2306 return (vino_drvdata->decoder.owner == vcs->channel);
2308 return (vino_drvdata->camera.owner == vcs->channel);
2314 static int vino_acquire_input(struct vino_channel_settings *vcs)
2318 dprintk("vino_acquire_input():\n");
2320 spin_lock(&vino_drvdata->input_lock);
2322 /* First try D1 and then SAA7191 */
2323 if (vino_drvdata->camera.driver
2324 && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
2325 if (i2c_use_client(vino_drvdata->camera.driver)) {
2330 vino_drvdata->camera.owner = vcs->channel;
2331 vcs->input = VINO_INPUT_D1;
2332 vcs->data_norm = VINO_DATA_NORM_D1;
2333 } else if (vino_drvdata->decoder.driver
2334 && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
2338 if (i2c_use_client(vino_drvdata->decoder.driver)) {
2343 vino_drvdata->decoder.owner = vcs->channel;
2344 vcs->input = VINO_INPUT_COMPOSITE;
2345 vcs->data_norm = VINO_DATA_NORM_PAL;
2347 saa7191_input = vino_get_saa7191_input(vcs->input);
2348 i2c_decoder_command(DECODER_SET_INPUT, &saa7191_input);
2350 saa7191_norm = vino_get_saa7191_norm(vcs->data_norm);
2351 i2c_decoder_command(DECODER_SAA7191_SET_NORM, &saa7191_norm);
2353 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2354 vino_drvdata->b.input : vino_drvdata->a.input;
2355 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2356 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2359 if (vcs->input == VINO_INPUT_NONE) {
2364 if (vino_is_input_owner(vcs)) {
2365 vino_set_default_clipping(vcs);
2366 vino_set_default_framerate(vcs);
2369 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2372 spin_unlock(&vino_drvdata->input_lock);
2377 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2379 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2380 &vino_drvdata->b : &vino_drvdata->a;
2383 dprintk("vino_set_input():\n");
2385 spin_lock(&vino_drvdata->input_lock);
2387 if (vcs->input == input)
2391 case VINO_INPUT_COMPOSITE:
2392 case VINO_INPUT_SVIDEO:
2393 if (!vino_drvdata->decoder.driver) {
2398 if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
2399 if (i2c_use_client(vino_drvdata->decoder.driver)) {
2403 vino_drvdata->decoder.owner = vcs->channel;
2406 if (vino_drvdata->decoder.owner == vcs->channel) {
2411 vcs->data_norm = VINO_DATA_NORM_PAL;
2413 saa7191_input = vino_get_saa7191_input(vcs->input);
2414 i2c_decoder_command(DECODER_SET_INPUT, &saa7191_input);
2415 saa7191_norm = vino_get_saa7191_norm(vcs->data_norm);
2416 i2c_decoder_command(DECODER_SAA7191_SET_NORM,
2419 if (vcs2->input != input) {
2425 vcs->data_norm = vcs2->data_norm;
2428 if (vino_drvdata->camera.owner == vcs->channel) {
2429 /* Transfer the ownership or release the input */
2430 if (vcs2->input == VINO_INPUT_D1) {
2431 vino_drvdata->camera.owner = vcs2->channel;
2433 i2c_release_client(vino_drvdata->
2435 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2440 if (!vino_drvdata->camera.driver) {
2445 if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
2446 if (i2c_use_client(vino_drvdata->camera.driver)) {
2450 vino_drvdata->camera.owner = vcs->channel;
2453 if (vino_drvdata->decoder.owner == vcs->channel) {
2454 /* Transfer the ownership or release the input */
2455 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2456 (vcs2->input == VINO_INPUT_SVIDEO)) {
2457 vino_drvdata->decoder.owner = vcs2->channel;
2459 i2c_release_client(vino_drvdata->
2461 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2466 vcs->data_norm = VINO_DATA_NORM_D1;
2473 vino_set_default_clipping(vcs);
2474 vino_set_default_framerate(vcs);
2476 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2479 spin_unlock(&vino_drvdata->input_lock);
2484 static void vino_release_input(struct vino_channel_settings *vcs)
2486 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2487 &vino_drvdata->b : &vino_drvdata->a;
2489 dprintk("vino_release_input():\n");
2491 spin_lock(&vino_drvdata->input_lock);
2493 /* Release ownership of the channel
2494 * and if the other channel takes input from
2495 * the same source, transfer the ownership */
2496 if (vino_drvdata->camera.owner == vcs->channel) {
2497 if (vcs2->input == VINO_INPUT_D1) {
2498 vino_drvdata->camera.owner = vcs2->channel;
2500 i2c_release_client(vino_drvdata->camera.driver);
2501 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2503 } else if (vino_drvdata->decoder.owner == vcs->channel) {
2504 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2505 (vcs2->input == VINO_INPUT_SVIDEO)) {
2506 vino_drvdata->decoder.owner = vcs2->channel;
2508 i2c_release_client(vino_drvdata->decoder.driver);
2509 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2512 vcs->input = VINO_INPUT_NONE;
2514 spin_unlock(&vino_drvdata->input_lock);
2517 /* execute with input_lock locked */
2518 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2519 unsigned int data_norm)
2523 switch (vcs->input) {
2525 /* only one "norm" supported */
2526 if (data_norm != VINO_DATA_NORM_D1)
2529 case VINO_INPUT_COMPOSITE:
2530 case VINO_INPUT_SVIDEO:
2532 saa7191_norm = vino_get_saa7191_norm(data_norm);
2534 i2c_decoder_command(DECODER_SAA7191_SET_NORM, &saa7191_norm);
2535 vcs->data_norm = data_norm;
2544 /* V4L2 helper functions */
2546 static int vino_find_data_format(__u32 pixelformat)
2550 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2551 if (vino_data_formats[i].pixelformat == pixelformat)
2555 return VINO_DATA_FMT_NONE;
2558 static int vino_enum_data_norm(struct vino_channel_settings *vcs, __u32 index)
2560 int data_norm = VINO_DATA_NORM_NONE;
2562 spin_lock(&vino_drvdata->input_lock);
2563 switch(vcs->input) {
2564 case VINO_INPUT_COMPOSITE:
2565 case VINO_INPUT_SVIDEO:
2567 data_norm = VINO_DATA_NORM_PAL;
2568 } else if (index == 1) {
2569 data_norm = VINO_DATA_NORM_NTSC;
2570 } else if (index == 2) {
2571 data_norm = VINO_DATA_NORM_SECAM;
2576 data_norm = VINO_DATA_NORM_D1;
2580 spin_unlock(&vino_drvdata->input_lock);
2585 static int vino_enum_input(struct vino_channel_settings *vcs, __u32 index)
2587 int input = VINO_INPUT_NONE;
2589 spin_lock(&vino_drvdata->input_lock);
2590 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2593 input = VINO_INPUT_COMPOSITE;
2596 input = VINO_INPUT_SVIDEO;
2599 input = VINO_INPUT_D1;
2602 } else if (vino_drvdata->decoder.driver) {
2605 input = VINO_INPUT_COMPOSITE;
2608 input = VINO_INPUT_SVIDEO;
2611 } else if (vino_drvdata->camera.driver) {
2614 input = VINO_INPUT_D1;
2618 spin_unlock(&vino_drvdata->input_lock);
2623 /* execute with input_lock locked */
2624 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2627 // FIXME: detect when no inputs available
2629 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2630 switch (vcs->input) {
2631 case VINO_INPUT_COMPOSITE:
2634 case VINO_INPUT_SVIDEO:
2641 } else if (vino_drvdata->decoder.driver) {
2642 switch (vcs->input) {
2643 case VINO_INPUT_COMPOSITE:
2646 case VINO_INPUT_SVIDEO:
2650 } else if (vino_drvdata->camera.driver) {
2651 switch (vcs->input) {
2663 static void vino_v4l2_querycap(struct v4l2_capability *cap)
2665 memset(cap, 0, sizeof(struct v4l2_capability));
2667 strcpy(cap->driver, vino_driver_name);
2668 strcpy(cap->card, vino_driver_description);
2669 strcpy(cap->bus_info, vino_bus_name);
2670 cap->version = VINO_VERSION_CODE;
2672 V4L2_CAP_VIDEO_CAPTURE |
2674 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2677 static int vino_v4l2_enuminput(struct vino_channel_settings *vcs,
2678 struct v4l2_input *i)
2680 __u32 index = i->index;
2682 dprintk("requested index = %d\n", index);
2684 input = vino_enum_input(vcs, index);
2685 if (input == VINO_INPUT_NONE)
2688 memset(i, 0, sizeof(struct v4l2_input));
2691 i->type = V4L2_INPUT_TYPE_CAMERA;
2692 i->std = vino_inputs[input].std;
2693 strcpy(i->name, vino_inputs[input].name);
2695 if ((input == VINO_INPUT_COMPOSITE)
2696 || (input == VINO_INPUT_SVIDEO)) {
2697 struct saa7191_status status;
2698 i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
2699 i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
2700 i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
2706 static int vino_v4l2_g_input(struct vino_channel_settings *vcs,
2707 struct v4l2_input *i)
2712 spin_lock(&vino_drvdata->input_lock);
2714 index = vino_find_input_index(vcs);
2715 spin_unlock(&vino_drvdata->input_lock);
2717 dprintk("input = %d\n", input);
2719 if (input == VINO_INPUT_NONE) {
2723 memset(i, 0, sizeof(struct v4l2_input));
2726 i->type = V4L2_INPUT_TYPE_CAMERA;
2727 i->std = vino_inputs[input].std;
2728 strcpy(i->name, vino_inputs[input].name);
2733 static int vino_v4l2_s_input(struct vino_channel_settings *vcs,
2734 struct v4l2_input *i)
2737 dprintk("requested input = %d\n", i->index);
2739 input = vino_enum_input(vcs, i->index);
2740 if (input == VINO_INPUT_NONE)
2743 return vino_set_input(vcs, input);
2746 static int vino_v4l2_enumstd(struct vino_channel_settings *vcs,
2747 struct v4l2_standard *s)
2749 int index = s->index;
2750 int data_norm = vino_enum_data_norm(vcs, index);
2751 dprintk("standard index = %d\n", index);
2753 if (data_norm == VINO_DATA_NORM_NONE)
2756 dprintk("standard name = %s\n",
2757 vino_data_norms[data_norm].description);
2759 memset(s, 0, sizeof(struct v4l2_standard));
2762 s->id = vino_data_norms[data_norm].std;
2763 s->frameperiod.numerator = 1;
2764 s->frameperiod.denominator =
2765 vino_data_norms[data_norm].fps_max;
2767 vino_data_norms[data_norm].framelines;
2769 vino_data_norms[data_norm].description);
2774 static int vino_v4l2_g_std(struct vino_channel_settings *vcs,
2777 spin_lock(&vino_drvdata->input_lock);
2778 dprintk("current standard = %d\n", vcs->data_norm);
2779 *std = vino_data_norms[vcs->data_norm].std;
2780 spin_unlock(&vino_drvdata->input_lock);
2785 static int vino_v4l2_s_std(struct vino_channel_settings *vcs,
2790 spin_lock(&vino_drvdata->input_lock);
2792 /* check if the standard is valid for the current input */
2793 if (vino_is_input_owner(vcs)
2794 && (vino_inputs[vcs->input].std & (*std))) {
2795 dprintk("standard accepted\n");
2797 /* change the video norm for SAA7191
2798 * and accept NTSC for D1 (do nothing) */
2800 if (vcs->input == VINO_INPUT_D1)
2803 if ((*std) & V4L2_STD_PAL) {
2804 vino_set_data_norm(vcs, VINO_DATA_NORM_PAL);
2805 vcs->data_norm = VINO_DATA_NORM_PAL;
2806 } else if ((*std) & V4L2_STD_NTSC) {
2807 vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC);
2808 vcs->data_norm = VINO_DATA_NORM_NTSC;
2809 } else if ((*std) & V4L2_STD_SECAM) {
2810 vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM);
2811 vcs->data_norm = VINO_DATA_NORM_SECAM;
2820 spin_unlock(&vino_drvdata->input_lock);
2825 static int vino_v4l2_enum_fmt(struct vino_channel_settings *vcs,
2826 struct v4l2_fmtdesc *fd)
2828 enum v4l2_buf_type type = fd->type;
2829 int index = fd->index;
2830 dprintk("format index = %d\n", index);
2833 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2834 if ((fd->index < 0) ||
2835 (fd->index >= VINO_DATA_FMT_COUNT))
2837 dprintk("format name = %s\n",
2838 vino_data_formats[index].description);
2840 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
2843 fd->pixelformat = vino_data_formats[index].pixelformat;
2844 strcpy(fd->description, vino_data_formats[index].description);
2846 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2854 static int vino_v4l2_try_fmt(struct vino_channel_settings *vcs,
2855 struct v4l2_format *f)
2857 struct vino_channel_settings tempvcs;
2860 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
2861 struct v4l2_pix_format *pf = &f->fmt.pix;
2863 dprintk("requested: w = %d, h = %d\n",
2864 pf->width, pf->height);
2866 spin_lock(&vino_drvdata->input_lock);
2867 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
2868 spin_unlock(&vino_drvdata->input_lock);
2870 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
2871 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
2872 tempvcs.data_format = VINO_DATA_FMT_RGB32;
2874 vino_data_formats[tempvcs.data_format].
2878 /* data format must be set before clipping/scaling */
2879 vino_set_scaling(&tempvcs, pf->width, pf->height);
2881 dprintk("data format = %s\n",
2882 vino_data_formats[tempvcs.data_format].description);
2884 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
2886 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
2889 pf->field = V4L2_FIELD_INTERLACED;
2890 pf->bytesperline = tempvcs.line_size;
2891 pf->sizeimage = tempvcs.line_size *
2892 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
2895 vino_data_formats[tempvcs.data_format].colorspace;
2900 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2908 static int vino_v4l2_g_fmt(struct vino_channel_settings *vcs,
2909 struct v4l2_format *f)
2912 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
2913 struct v4l2_pix_format *pf = &f->fmt.pix;
2914 spin_lock(&vino_drvdata->input_lock);
2916 pf->width = (vcs->clipping.right - vcs->clipping.left) /
2918 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
2921 vino_data_formats[vcs->data_format].pixelformat;
2923 pf->field = V4L2_FIELD_INTERLACED;
2924 pf->bytesperline = vcs->line_size;
2925 pf->sizeimage = vcs->line_size *
2926 (vcs->clipping.bottom - vcs->clipping.top) /
2929 vino_data_formats[vcs->data_format].colorspace;
2933 spin_unlock(&vino_drvdata->input_lock);
2936 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2944 static int vino_v4l2_s_fmt(struct vino_channel_settings *vcs,
2945 struct v4l2_format *f)
2950 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
2951 struct v4l2_pix_format *pf = &f->fmt.pix;
2952 spin_lock(&vino_drvdata->input_lock);
2954 if (!vino_is_input_owner(vcs)) {
2955 spin_unlock(&vino_drvdata->input_lock);
2959 data_format = vino_find_data_format(pf->pixelformat);
2960 if (data_format == VINO_DATA_FMT_NONE) {
2961 vcs->data_format = VINO_DATA_FMT_RGB32;
2963 vino_data_formats[vcs->data_format].
2966 vcs->data_format = data_format;
2969 /* data format must be set before clipping/scaling */
2970 vino_set_scaling(vcs, pf->width, pf->height);
2972 dprintk("data format = %s\n",
2973 vino_data_formats[vcs->data_format].description);
2975 pf->width = vcs->clipping.right - vcs->clipping.left;
2976 pf->height = vcs->clipping.bottom - vcs->clipping.top;
2978 pf->field = V4L2_FIELD_INTERLACED;
2979 pf->bytesperline = vcs->line_size;
2980 pf->sizeimage = vcs->line_size *
2981 (vcs->clipping.bottom - vcs->clipping.top) /
2984 vino_data_formats[vcs->data_format].colorspace;
2988 spin_unlock(&vino_drvdata->input_lock);
2991 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2999 static int vino_v4l2_cropcap(struct vino_channel_settings *vcs,
3000 struct v4l2_cropcap *ccap)
3002 const struct vino_data_norm *norm;
3004 switch (ccap->type) {
3005 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3006 spin_lock(&vino_drvdata->input_lock);
3007 norm = &vino_data_norms[vcs->data_norm];
3008 spin_unlock(&vino_drvdata->input_lock);
3010 ccap->bounds.left = 0;
3011 ccap->bounds.top = 0;
3012 ccap->bounds.width = norm->width;
3013 ccap->bounds.height = norm->height;
3014 memcpy(&ccap->defrect, &ccap->bounds,
3015 sizeof(struct v4l2_rect));
3017 ccap->pixelaspect.numerator = 1;
3018 ccap->pixelaspect.denominator = 1;
3020 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3028 static int vino_v4l2_g_crop(struct vino_channel_settings *vcs,
3029 struct v4l2_crop *c)
3032 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3033 spin_lock(&vino_drvdata->input_lock);
3035 c->c.left = vcs->clipping.left;
3036 c->c.top = vcs->clipping.top;
3037 c->c.width = vcs->clipping.right - vcs->clipping.left;
3038 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3040 spin_unlock(&vino_drvdata->input_lock);
3042 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3050 static int vino_v4l2_s_crop(struct vino_channel_settings *vcs,
3051 struct v4l2_crop *c)
3054 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3055 spin_lock(&vino_drvdata->input_lock);
3057 if (!vino_is_input_owner(vcs)) {
3058 spin_unlock(&vino_drvdata->input_lock);
3061 vino_set_clipping(vcs, c->c.left, c->c.top,
3062 c->c.width, c->c.height);
3064 spin_unlock(&vino_drvdata->input_lock);
3066 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3074 static int vino_v4l2_g_parm(struct vino_channel_settings *vcs,
3075 struct v4l2_streamparm *sp)
3078 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3079 struct v4l2_captureparm *cp = &sp->parm.capture;
3080 memset(cp, 0, sizeof(struct v4l2_captureparm));
3082 cp->capability = V4L2_CAP_TIMEPERFRAME;
3083 cp->timeperframe.numerator = 1;
3085 spin_lock(&vino_drvdata->input_lock);
3086 cp->timeperframe.denominator = vcs->fps;
3087 spin_unlock(&vino_drvdata->input_lock);
3089 // TODO: cp->readbuffers = xxx;
3092 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3100 static int vino_v4l2_s_parm(struct vino_channel_settings *vcs,
3101 struct v4l2_streamparm *sp)
3104 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3105 struct v4l2_captureparm *cp = &sp->parm.capture;
3107 spin_lock(&vino_drvdata->input_lock);
3108 if (!vino_is_input_owner(vcs)) {
3109 spin_unlock(&vino_drvdata->input_lock);
3113 if ((cp->timeperframe.numerator == 0) ||
3114 (cp->timeperframe.denominator == 0)) {
3115 /* reset framerate */
3116 vino_set_default_framerate(vcs);
3118 vino_set_framerate(vcs, cp->timeperframe.denominator /
3119 cp->timeperframe.numerator);
3121 spin_unlock(&vino_drvdata->input_lock);
3123 // TODO: set buffers according to cp->readbuffers
3126 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3134 static int vino_v4l2_reqbufs(struct vino_channel_settings *vcs,
3135 struct v4l2_requestbuffers *rb)
3141 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3142 // TODO: check queue type
3143 if (rb->memory != V4L2_MEMORY_MMAP) {
3144 dprintk("type not mmap\n");
3148 if (vino_is_capturing(vcs)) {
3149 dprintk("busy, capturing\n");
3153 dprintk("count = %d\n", rb->count);
3154 if (rb->count > 0) {
3155 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3156 dprintk("busy, buffers still mapped\n");
3159 vino_queue_free(&vcs->fb_queue);
3160 vino_queue_init(&vcs->fb_queue, &rb->count);
3163 vino_capture_stop(vcs);
3164 vino_queue_free(&vcs->fb_queue);
3168 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3176 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3177 struct vino_framebuffer *fb,
3178 struct v4l2_buffer *b)
3180 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3182 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3183 b->flags |= V4L2_BUF_FLAG_DONE;
3184 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3186 b->flags &= ~V4L2_BUF_FLAG_DONE;
3187 b->flags |= V4L2_BUF_FLAG_QUEUED;
3189 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3190 V4L2_BUF_FLAG_QUEUED);
3193 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3195 if (fb->map_count > 0)
3196 b->flags |= V4L2_BUF_FLAG_MAPPED;
3199 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3200 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3201 b->m.offset = fb->offset;
3202 b->bytesused = fb->data_size;
3203 b->length = fb->size;
3204 b->field = V4L2_FIELD_INTERLACED;
3205 b->sequence = fb->frame_counter;
3206 memcpy(&b->timestamp, &fb->timestamp,
3207 sizeof(struct timeval));
3210 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3211 fb->id, fb->size, fb->data_size, fb->offset);
3214 static int vino_v4l2_querybuf(struct vino_channel_settings *vcs,
3215 struct v4l2_buffer *b)
3221 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3222 struct vino_framebuffer *fb;
3224 // TODO: check queue type
3225 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3226 dprintk("invalid index = %d\n",
3231 fb = vino_queue_get_buffer(&vcs->fb_queue,
3234 dprintk("vino_queue_get_buffer() failed");
3238 vino_v4l2_get_buffer_status(vcs, fb, b);
3241 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3249 static int vino_v4l2_qbuf(struct vino_channel_settings *vcs,
3250 struct v4l2_buffer *b)
3256 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3257 struct vino_framebuffer *fb;
3260 // TODO: check queue type
3261 if (b->memory != V4L2_MEMORY_MMAP) {
3262 dprintk("type not mmap\n");
3266 fb = vino_capture_enqueue(vcs, b->index);
3270 vino_v4l2_get_buffer_status(vcs, fb, b);
3272 if (vcs->streaming) {
3273 ret = vino_capture_next(vcs, 1);
3279 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3287 static int vino_v4l2_dqbuf(struct vino_channel_settings *vcs,
3288 struct v4l2_buffer *b,
3289 unsigned int nonblocking)
3295 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3296 struct vino_framebuffer *fb;
3297 unsigned int incoming, outgoing;
3300 // TODO: check queue type
3302 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3304 dprintk("vino_queue_get_incoming() failed\n");
3307 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3309 dprintk("vino_queue_get_outgoing() failed\n");
3313 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3315 if (outgoing == 0) {
3316 if (incoming == 0) {
3317 dprintk("no incoming or outgoing buffers\n");
3321 dprintk("non-blocking I/O was selected and "
3322 "there are no buffers to dequeue\n");
3326 err = vino_wait_for_frame(vcs);
3328 err = vino_wait_for_frame(vcs);
3331 vino_capture_failed(vcs);
3337 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3339 dprintk("vino_queue_remove() failed\n");
3343 err = vino_check_buffer(vcs, fb);
3347 vino_v4l2_get_buffer_status(vcs, fb, b);
3350 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3358 static int vino_v4l2_streamon(struct vino_channel_settings *vcs)
3360 unsigned int incoming;
3368 // TODO: check queue type
3370 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3371 dprintk("no buffers allocated\n");
3375 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3377 dprintk("vino_queue_get_incoming() failed\n");
3384 ret = vino_capture_next(vcs, 1);
3388 dprintk("couldn't start capture\n");
3396 static int vino_v4l2_streamoff(struct vino_channel_settings *vcs)
3401 if (!vcs->streaming)
3404 vino_capture_stop(vcs);
3410 static int vino_v4l2_queryctrl(struct vino_channel_settings *vcs,
3411 struct v4l2_queryctrl *queryctrl)
3416 spin_lock(&vino_drvdata->input_lock);
3418 switch (vcs->input) {
3420 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3421 if (vino_indycam_v4l2_controls[i].id ==
3424 &vino_indycam_v4l2_controls[i],
3425 sizeof(struct v4l2_queryctrl));
3432 case VINO_INPUT_COMPOSITE:
3433 case VINO_INPUT_SVIDEO:
3434 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3435 if (vino_saa7191_v4l2_controls[i].id ==
3438 &vino_saa7191_v4l2_controls[i],
3439 sizeof(struct v4l2_queryctrl));
3451 spin_unlock(&vino_drvdata->input_lock);
3456 static int vino_v4l2_g_ctrl(struct vino_channel_settings *vcs,
3457 struct v4l2_control *control)
3459 struct indycam_control indycam_ctrl;
3460 struct saa7191_control saa7191_ctrl;
3463 spin_lock(&vino_drvdata->input_lock);
3465 switch (vcs->input) {
3467 i2c_camera_command(DECODER_INDYCAM_GET_CONTROLS,
3470 switch(control->id) {
3471 case V4L2_CID_AUTOGAIN:
3472 control->value = indycam_ctrl.agc;
3474 case V4L2_CID_AUTO_WHITE_BALANCE:
3475 control->value = indycam_ctrl.awb;
3478 control->value = indycam_ctrl.gain;
3480 case V4L2_CID_PRIVATE_BASE:
3481 control->value = indycam_ctrl.red_saturation;
3483 case V4L2_CID_PRIVATE_BASE + 1:
3484 control->value = indycam_ctrl.blue_saturation;
3486 case V4L2_CID_RED_BALANCE:
3487 control->value = indycam_ctrl.red_balance;
3489 case V4L2_CID_BLUE_BALANCE:
3490 control->value = indycam_ctrl.blue_balance;
3492 case V4L2_CID_EXPOSURE:
3493 control->value = indycam_ctrl.shutter;
3495 case V4L2_CID_GAMMA:
3496 control->value = indycam_ctrl.gamma;
3502 case VINO_INPUT_COMPOSITE:
3503 case VINO_INPUT_SVIDEO:
3504 i2c_decoder_command(DECODER_SAA7191_GET_CONTROLS,
3507 switch(control->id) {
3509 control->value = saa7191_ctrl.hue;
3511 case V4L2_CID_PRIVATE_BASE:
3512 control->value = saa7191_ctrl.vtrc;
3522 spin_unlock(&vino_drvdata->input_lock);
3527 static int vino_v4l2_s_ctrl(struct vino_channel_settings *vcs,
3528 struct v4l2_control *control)
3530 struct indycam_control indycam_ctrl;
3531 struct saa7191_control saa7191_ctrl;
3535 spin_lock(&vino_drvdata->input_lock);
3537 switch (vcs->input) {
3539 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3540 if (vino_indycam_v4l2_controls[i].id ==
3542 if ((control->value >=
3543 vino_indycam_v4l2_controls[i].minimum)
3544 && (control->value <=
3545 vino_indycam_v4l2_controls[i].
3558 indycam_ctrl.agc = INDYCAM_VALUE_UNCHANGED;
3559 indycam_ctrl.awb = INDYCAM_VALUE_UNCHANGED;
3560 indycam_ctrl.shutter = INDYCAM_VALUE_UNCHANGED;
3561 indycam_ctrl.gain = INDYCAM_VALUE_UNCHANGED;
3562 indycam_ctrl.red_balance = INDYCAM_VALUE_UNCHANGED;
3563 indycam_ctrl.blue_balance = INDYCAM_VALUE_UNCHANGED;
3564 indycam_ctrl.red_saturation = INDYCAM_VALUE_UNCHANGED;
3565 indycam_ctrl.blue_saturation = INDYCAM_VALUE_UNCHANGED;
3566 indycam_ctrl.gamma = INDYCAM_VALUE_UNCHANGED;
3568 switch(control->id) {
3569 case V4L2_CID_AUTOGAIN:
3570 indycam_ctrl.agc = control->value;
3572 case V4L2_CID_AUTO_WHITE_BALANCE:
3573 indycam_ctrl.awb = control->value;
3576 indycam_ctrl.gain = control->value;
3578 case V4L2_CID_PRIVATE_BASE:
3579 indycam_ctrl.red_saturation = control->value;
3581 case V4L2_CID_PRIVATE_BASE + 1:
3582 indycam_ctrl.blue_saturation = control->value;
3584 case V4L2_CID_RED_BALANCE:
3585 indycam_ctrl.red_balance = control->value;
3587 case V4L2_CID_BLUE_BALANCE:
3588 indycam_ctrl.blue_balance = control->value;
3590 case V4L2_CID_EXPOSURE:
3591 indycam_ctrl.shutter = control->value;
3593 case V4L2_CID_GAMMA:
3594 indycam_ctrl.gamma = control->value;
3601 i2c_camera_command(DECODER_INDYCAM_SET_CONTROLS,
3604 case VINO_INPUT_COMPOSITE:
3605 case VINO_INPUT_SVIDEO:
3606 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3607 if (vino_saa7191_v4l2_controls[i].id ==
3609 if ((control->value >=
3610 vino_saa7191_v4l2_controls[i].minimum)
3611 && (control->value <=
3612 vino_saa7191_v4l2_controls[i].
3625 saa7191_ctrl.hue = SAA7191_VALUE_UNCHANGED;
3626 saa7191_ctrl.vtrc = SAA7191_VALUE_UNCHANGED;
3628 switch(control->id) {
3630 saa7191_ctrl.hue = control->value;
3632 case V4L2_CID_PRIVATE_BASE:
3633 saa7191_ctrl.vtrc = control->value;
3640 i2c_decoder_command(DECODER_SAA7191_SET_CONTROLS,
3648 spin_unlock(&vino_drvdata->input_lock);
3653 /* File operations */
3655 static int vino_open(struct inode *inode, struct file *file)
3657 struct video_device *dev = video_devdata(file);
3658 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3660 dprintk("open(): channel = %c\n",
3661 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3666 dprintk("open(): driver busy\n");
3671 ret = vino_acquire_input(vcs);
3673 dprintk("open(): vino_acquire_input() failed\n");
3682 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3687 static int vino_close(struct inode *inode, struct file *file)
3689 struct video_device *dev = video_devdata(file);
3690 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3691 dprintk("close():\n");
3698 vino_release_input(vcs);
3700 /* stop DMA and free buffers */
3701 vino_capture_stop(vcs);
3702 vino_queue_free(&vcs->fb_queue);
3710 static void vino_vm_open(struct vm_area_struct *vma)
3712 struct vino_framebuffer *fb = vma->vm_private_data;
3715 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3718 static void vino_vm_close(struct vm_area_struct *vma)
3720 struct vino_framebuffer *fb = vma->vm_private_data;
3723 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3726 static struct vm_operations_struct vino_vm_ops = {
3727 .open = vino_vm_open,
3728 .close = vino_vm_close,
3731 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3733 struct video_device *dev = video_devdata(file);
3734 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3736 unsigned long start = vma->vm_start;
3737 unsigned long size = vma->vm_end - vma->vm_start;
3738 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3740 struct vino_framebuffer *fb = NULL;
3741 unsigned int i, length;
3744 dprintk("mmap():\n");
3746 // TODO: reject mmap if already mapped
3748 if (down_interruptible(&vcs->sem))
3756 // TODO: check queue type
3758 if (!(vma->vm_flags & VM_WRITE)) {
3759 dprintk("mmap(): app bug: PROT_WRITE please\n");
3763 if (!(vma->vm_flags & VM_SHARED)) {
3764 dprintk("mmap(): app bug: MAP_SHARED please\n");
3769 /* find the correct buffer using offset */
3770 length = vino_queue_get_length(&vcs->fb_queue);
3772 dprintk("mmap(): queue not initialized\n");
3777 for (i = 0; i < length; i++) {
3778 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3780 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3785 if (fb->offset == offset)
3789 dprintk("mmap(): invalid offset = %lu\n", offset);
3794 dprintk("mmap(): buffer = %d\n", i);
3796 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3797 dprintk("mmap(): failed: size = %lu > %lu\n",
3798 size, fb->desc_table.page_count * PAGE_SIZE);
3803 for (i = 0; i < fb->desc_table.page_count; i++) {
3805 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3808 if (size < PAGE_SIZE)
3811 // protection was: PAGE_READONLY
3812 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3813 vma->vm_page_prot)) {
3814 dprintk("mmap(): remap_pfn_range() failed\n");
3825 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3826 vma->vm_flags &= ~VM_IO;
3827 vma->vm_private_data = fb;
3828 vma->vm_file = file;
3829 vma->vm_ops = &vino_vm_ops;
3837 static unsigned int vino_poll(struct file *file, poll_table *pt)
3839 struct video_device *dev = video_devdata(file);
3840 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3841 unsigned int outgoing;
3842 unsigned int ret = 0;
3845 // TODO: this has to be corrected for different read modes
3847 dprintk("poll():\n");
3849 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3850 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3857 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3859 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3860 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3866 dprintk("poll(): data %savailable\n",
3867 (outgoing > 0) ? "" : "not ");
3869 ret = POLLIN | POLLRDNORM;
3877 static int vino_do_ioctl(struct inode *inode, struct file *file,
3878 unsigned int cmd, void *arg)
3880 struct video_device *dev = video_devdata(file);
3881 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3883 switch (_IOC_TYPE(cmd)) {
3885 dprintk("ioctl(): V4L1 unsupported (0x%08x)\n", cmd);
3888 dprintk("ioctl(): V4L2 %s (0x%08x)\n",
3889 v4l2_ioctl_names[_IOC_NR(cmd)], cmd);
3892 dprintk("ioctl(): unsupported command 0x%08x\n", cmd);
3896 /* TODO: V4L1 interface (use compatibility layer?) */
3897 /* V4L2 interface */
3898 case VIDIOC_QUERYCAP: {
3899 vino_v4l2_querycap(arg);
3902 case VIDIOC_ENUMINPUT: {
3903 return vino_v4l2_enuminput(vcs, arg);
3905 case VIDIOC_G_INPUT: {
3906 return vino_v4l2_g_input(vcs, arg);
3908 case VIDIOC_S_INPUT: {
3909 return vino_v4l2_s_input(vcs, arg);
3911 case VIDIOC_ENUMSTD: {
3912 return vino_v4l2_enumstd(vcs, arg);
3914 case VIDIOC_G_STD: {
3915 return vino_v4l2_g_std(vcs, arg);
3917 case VIDIOC_S_STD: {
3918 return vino_v4l2_s_std(vcs, arg);
3920 case VIDIOC_ENUM_FMT: {
3921 return vino_v4l2_enum_fmt(vcs, arg);
3923 case VIDIOC_TRY_FMT: {
3924 return vino_v4l2_try_fmt(vcs, arg);
3926 case VIDIOC_G_FMT: {
3927 return vino_v4l2_g_fmt(vcs, arg);
3929 case VIDIOC_S_FMT: {
3930 return vino_v4l2_s_fmt(vcs, arg);
3932 case VIDIOC_CROPCAP: {
3933 return vino_v4l2_cropcap(vcs, arg);
3935 case VIDIOC_G_CROP: {
3936 return vino_v4l2_g_crop(vcs, arg);
3938 case VIDIOC_S_CROP: {
3939 return vino_v4l2_s_crop(vcs, arg);
3941 case VIDIOC_G_PARM: {
3942 return vino_v4l2_g_parm(vcs, arg);
3944 case VIDIOC_S_PARM: {
3945 return vino_v4l2_s_parm(vcs, arg);
3947 case VIDIOC_REQBUFS: {
3948 return vino_v4l2_reqbufs(vcs, arg);
3950 case VIDIOC_QUERYBUF: {
3951 return vino_v4l2_querybuf(vcs, arg);
3954 return vino_v4l2_qbuf(vcs, arg);
3956 case VIDIOC_DQBUF: {
3957 return vino_v4l2_dqbuf(vcs, arg, file->f_flags & O_NONBLOCK);
3959 case VIDIOC_STREAMON: {
3960 return vino_v4l2_streamon(vcs);
3962 case VIDIOC_STREAMOFF: {
3963 return vino_v4l2_streamoff(vcs);
3965 case VIDIOC_QUERYCTRL: {
3966 return vino_v4l2_queryctrl(vcs, arg);
3968 case VIDIOC_G_CTRL: {
3969 return vino_v4l2_g_ctrl(vcs, arg);
3971 case VIDIOC_S_CTRL: {
3972 return vino_v4l2_s_ctrl(vcs, arg);
3975 return -ENOIOCTLCMD;
3981 static int vino_ioctl(struct inode *inode, struct file *file,
3982 unsigned int cmd, unsigned long arg)
3984 struct video_device *dev = video_devdata(file);
3985 struct vino_channel_settings *vcs = video_get_drvdata(dev);
3988 if (down_interruptible(&vcs->sem))
3991 ret = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
3998 /* Initialization and cleanup */
4001 static int vino_init_stage = 0;
4003 static struct file_operations vino_fops = {
4004 .owner = THIS_MODULE,
4006 .release = vino_close,
4007 .ioctl = vino_ioctl,
4010 .llseek = no_llseek,
4013 static struct video_device v4l_device_template = {
4015 //.type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE |
4016 // VID_TYPE_CLIPPING | VID_TYPE_SCALES, VID_TYPE_OVERLAY
4017 .hardware = VID_HARDWARE_VINO,
4022 static void vino_module_cleanup(int stage)
4026 video_unregister_device(vino_drvdata->b.v4l_device);
4027 vino_drvdata->b.v4l_device = NULL;
4029 video_unregister_device(vino_drvdata->a.v4l_device);
4030 vino_drvdata->a.v4l_device = NULL;
4034 free_irq(SGI_VINO_IRQ, NULL);
4036 if (vino_drvdata->b.v4l_device) {
4037 video_device_release(vino_drvdata->b.v4l_device);
4038 vino_drvdata->b.v4l_device = NULL;
4041 if (vino_drvdata->a.v4l_device) {
4042 video_device_release(vino_drvdata->a.v4l_device);
4043 vino_drvdata->a.v4l_device = NULL;
4046 /* all entries in dma_cpu dummy table have the same address */
4047 dma_unmap_single(NULL,
4048 vino_drvdata->dummy_desc_table.dma_cpu[0],
4049 PAGE_SIZE, DMA_FROM_DEVICE);
4050 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4051 * sizeof(dma_addr_t),
4052 (void *)vino_drvdata->
4053 dummy_desc_table.dma_cpu,
4054 vino_drvdata->dummy_desc_table.dma);
4056 free_page(vino_drvdata->dummy_page);
4058 kfree(vino_drvdata);
4064 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4069 static int vino_probe(void)
4071 unsigned long rev_id;
4073 if (ip22_is_fullhouse()) {
4074 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4078 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4079 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4083 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4085 printk(KERN_ERR "VINO: ioremap() failed\n");
4090 if (get_dbe(rev_id, &(vino->rev_id))) {
4091 printk(KERN_ERR "Failed to read VINO revision register\n");
4092 vino_module_cleanup(vino_init_stage);
4096 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4097 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4099 vino_module_cleanup(vino_init_stage);
4103 printk(KERN_INFO "VINO with chip ID %ld, revision %ld found\n",
4104 VINO_ID_VALUE(rev_id), VINO_REV_NUM(rev_id));
4109 static int vino_init(void)
4111 dma_addr_t dma_dummy_address;
4114 vino_drvdata = (struct vino_settings *)
4115 kmalloc(sizeof(struct vino_settings), GFP_KERNEL);
4116 if (!vino_drvdata) {
4117 vino_module_cleanup(vino_init_stage);
4120 memset(vino_drvdata, 0, sizeof(struct vino_settings));
4123 /* create a dummy dma descriptor */
4124 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4125 if (!vino_drvdata->dummy_page) {
4126 vino_module_cleanup(vino_init_stage);
4131 // TODO: use page_count in dummy_desc_table
4133 vino_drvdata->dummy_desc_table.dma_cpu =
4134 dma_alloc_coherent(NULL,
4135 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4136 &vino_drvdata->dummy_desc_table.dma,
4137 GFP_KERNEL | GFP_DMA);
4138 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4139 vino_module_cleanup(vino_init_stage);
4144 dma_dummy_address = dma_map_single(NULL,
4145 (void *)vino_drvdata->dummy_page,
4146 PAGE_SIZE, DMA_FROM_DEVICE);
4147 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4148 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4151 /* initialize VINO */
4154 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4155 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4156 udelay(VINO_DESC_FETCH_DELAY);
4158 vino->intr_status = 0;
4160 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4161 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4166 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4167 unsigned int channel, const char *name)
4169 vcs->channel = channel;
4170 vcs->input = VINO_INPUT_NONE;
4173 vcs->data_format = VINO_DATA_FMT_GREY;
4174 vcs->data_norm = VINO_DATA_NORM_NTSC;
4175 vcs->decimation = 1;
4176 vino_set_default_clipping(vcs);
4177 vino_set_default_framerate(vcs);
4181 init_MUTEX(&vcs->sem);
4182 spin_lock_init(&vcs->capture_lock);
4184 init_MUTEX(&vcs->fb_queue.queue_sem);
4185 spin_lock_init(&vcs->fb_queue.queue_lock);
4186 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4188 vcs->v4l_device = video_device_alloc();
4189 if (!vcs->v4l_device) {
4190 vino_module_cleanup(vino_init_stage);
4195 memcpy(vcs->v4l_device, &v4l_device_template,
4196 sizeof(struct video_device));
4197 strcpy(vcs->v4l_device->name, name);
4198 vcs->v4l_device->release = video_device_release;
4200 video_set_drvdata(vcs->v4l_device, vcs);
4205 static int __init vino_module_init(void)
4209 printk(KERN_INFO "SGI VINO driver version %s\n",
4210 VINO_MODULE_VERSION);
4220 /* initialize data structures */
4222 spin_lock_init(&vino_drvdata->vino_lock);
4223 spin_lock_init(&vino_drvdata->input_lock);
4225 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4226 vino_v4l_device_name_a);
4230 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4231 vino_v4l_device_name_b);
4235 /* initialize hardware and register V4L devices */
4237 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4238 vino_driver_description, NULL);
4240 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4242 vino_module_cleanup(vino_init_stage);
4247 ret = vino_i2c_add_bus();
4249 printk(KERN_ERR "VINO I2C bus registration failed\n");
4250 vino_module_cleanup(vino_init_stage);
4255 ret = video_register_device(vino_drvdata->a.v4l_device,
4256 VFL_TYPE_GRABBER, -1);
4258 printk(KERN_ERR "VINO channel A Video4Linux-device "
4259 "registration failed\n");
4260 vino_module_cleanup(vino_init_stage);
4265 ret = video_register_device(vino_drvdata->b.v4l_device,
4266 VFL_TYPE_GRABBER, -1);
4268 printk(KERN_ERR "VINO channel B Video4Linux-device "
4269 "registration failed\n");
4270 vino_module_cleanup(vino_init_stage);
4275 #if defined(CONFIG_KMOD) && defined(MODULE)
4276 request_module("saa7191");
4277 request_module("indycam");
4280 dprintk("init complete!\n");
4285 static void __exit vino_module_exit(void)
4287 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4288 vino_module_cleanup(vino_init_stage);
4289 dprintk("cleanup complete, exit!\n");
4292 module_init(vino_module_init);
4293 module_exit(vino_module_exit);