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-device.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 v4l2_device v4l2_dev;
299 struct vino_channel_settings a;
300 struct vino_channel_settings b;
302 struct vino_client decoder;
303 struct vino_client camera;
305 /* a lock for vino register access */
306 spinlock_t vino_lock;
307 /* a lock for channel input changes */
308 spinlock_t input_lock;
310 unsigned long dummy_page;
311 struct vino_descriptor_table dummy_desc_table;
314 /* Module parameters */
317 * Using vino_pixel_conversion the ABGR32-format pixels supplied
318 * by the VINO chip can be converted to more common formats
319 * like RGBA32 (or probably RGB24 in the future). This way we
320 * can give out data that can be specified correctly with
321 * the V4L2-definitions.
323 * The pixel format is specified as RGBA32 when no conversion
326 * Note that this only affects the 32-bit bit depth.
328 * Use non-zero value to enable conversion.
330 static int vino_pixel_conversion;
332 module_param_named(pixelconv, vino_pixel_conversion, int, 0);
334 MODULE_PARM_DESC(pixelconv,
335 "enable pixel conversion (non-zero value enables)");
337 /* Internal data structures */
339 static struct sgi_vino *vino;
341 static struct vino_settings *vino_drvdata;
343 static const char *vino_driver_name = "vino";
344 static const char *vino_driver_description = "SGI VINO";
345 static const char *vino_bus_name = "GIO64 bus";
346 static const char *vino_vdev_name_a = "SGI VINO Channel A";
347 static const char *vino_vdev_name_b = "SGI VINO Channel B";
349 static void vino_capture_tasklet(unsigned long channel);
351 DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
352 DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
354 static const struct vino_input vino_inputs[] = {
357 .std = V4L2_STD_NTSC | V4L2_STD_PAL
361 .std = V4L2_STD_NTSC | V4L2_STD_PAL
364 .name = "D1/IndyCam",
365 .std = V4L2_STD_NTSC,
369 static const struct vino_data_format vino_data_formats[] = {
371 .description = "8-bit greyscale",
373 .pixelformat = V4L2_PIX_FMT_GREY,
374 .colorspace = V4L2_COLORSPACE_SMPTE170M,
376 .description = "8-bit dithered RGB 3-3-2",
378 .pixelformat = V4L2_PIX_FMT_RGB332,
379 .colorspace = V4L2_COLORSPACE_SRGB,
381 .description = "32-bit RGB",
383 .pixelformat = V4L2_PIX_FMT_RGB32,
384 .colorspace = V4L2_COLORSPACE_SRGB,
386 .description = "YUV 4:2:2",
388 .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
389 .colorspace = V4L2_COLORSPACE_SMPTE170M,
393 static const struct vino_data_norm vino_data_norms[] = {
395 .description = "NTSC",
396 .std = V4L2_STD_NTSC,
400 .width = VINO_NTSC_WIDTH,
401 .height = VINO_NTSC_HEIGHT,
403 .top = VINO_CLIPPING_START_ODD_NTSC,
405 .bottom = VINO_CLIPPING_START_ODD_NTSC
406 + VINO_NTSC_HEIGHT / 2 - 1,
407 .right = VINO_NTSC_WIDTH,
410 .top = VINO_CLIPPING_START_EVEN_NTSC,
412 .bottom = VINO_CLIPPING_START_EVEN_NTSC
413 + VINO_NTSC_HEIGHT / 2 - 1,
414 .right = VINO_NTSC_WIDTH,
417 .description = "PAL",
422 .width = VINO_PAL_WIDTH,
423 .height = VINO_PAL_HEIGHT,
425 .top = VINO_CLIPPING_START_ODD_PAL,
427 .bottom = VINO_CLIPPING_START_ODD_PAL
428 + VINO_PAL_HEIGHT / 2 - 1,
429 .right = VINO_PAL_WIDTH,
432 .top = VINO_CLIPPING_START_EVEN_PAL,
434 .bottom = VINO_CLIPPING_START_EVEN_PAL
435 + VINO_PAL_HEIGHT / 2 - 1,
436 .right = VINO_PAL_WIDTH,
439 .description = "SECAM",
440 .std = V4L2_STD_SECAM,
444 .width = VINO_PAL_WIDTH,
445 .height = VINO_PAL_HEIGHT,
447 .top = VINO_CLIPPING_START_ODD_PAL,
449 .bottom = VINO_CLIPPING_START_ODD_PAL
450 + VINO_PAL_HEIGHT / 2 - 1,
451 .right = VINO_PAL_WIDTH,
454 .top = VINO_CLIPPING_START_EVEN_PAL,
456 .bottom = VINO_CLIPPING_START_EVEN_PAL
457 + VINO_PAL_HEIGHT / 2 - 1,
458 .right = VINO_PAL_WIDTH,
461 .description = "NTSC/D1",
462 .std = V4L2_STD_NTSC,
466 .width = VINO_NTSC_WIDTH,
467 .height = VINO_NTSC_HEIGHT,
469 .top = VINO_CLIPPING_START_ODD_D1,
471 .bottom = VINO_CLIPPING_START_ODD_D1
472 + VINO_NTSC_HEIGHT / 2 - 1,
473 .right = VINO_NTSC_WIDTH,
476 .top = VINO_CLIPPING_START_EVEN_D1,
478 .bottom = VINO_CLIPPING_START_EVEN_D1
479 + VINO_NTSC_HEIGHT / 2 - 1,
480 .right = VINO_NTSC_WIDTH,
485 #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
487 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
489 .id = V4L2_CID_AUTOGAIN,
490 .type = V4L2_CTRL_TYPE_BOOLEAN,
491 .name = "Automatic Gain Control",
495 .default_value = INDYCAM_AGC_DEFAULT,
497 .id = V4L2_CID_AUTO_WHITE_BALANCE,
498 .type = V4L2_CTRL_TYPE_BOOLEAN,
499 .name = "Automatic White Balance",
503 .default_value = INDYCAM_AWB_DEFAULT,
506 .type = V4L2_CTRL_TYPE_INTEGER,
508 .minimum = INDYCAM_GAIN_MIN,
509 .maximum = INDYCAM_GAIN_MAX,
511 .default_value = INDYCAM_GAIN_DEFAULT,
513 .id = INDYCAM_CONTROL_RED_SATURATION,
514 .type = V4L2_CTRL_TYPE_INTEGER,
515 .name = "Red Saturation",
516 .minimum = INDYCAM_RED_SATURATION_MIN,
517 .maximum = INDYCAM_RED_SATURATION_MAX,
519 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
521 .id = INDYCAM_CONTROL_BLUE_SATURATION,
522 .type = V4L2_CTRL_TYPE_INTEGER,
523 .name = "Blue Saturation",
524 .minimum = INDYCAM_BLUE_SATURATION_MIN,
525 .maximum = INDYCAM_BLUE_SATURATION_MAX,
527 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
529 .id = V4L2_CID_RED_BALANCE,
530 .type = V4L2_CTRL_TYPE_INTEGER,
531 .name = "Red Balance",
532 .minimum = INDYCAM_RED_BALANCE_MIN,
533 .maximum = INDYCAM_RED_BALANCE_MAX,
535 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
537 .id = V4L2_CID_BLUE_BALANCE,
538 .type = V4L2_CTRL_TYPE_INTEGER,
539 .name = "Blue Balance",
540 .minimum = INDYCAM_BLUE_BALANCE_MIN,
541 .maximum = INDYCAM_BLUE_BALANCE_MAX,
543 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
545 .id = V4L2_CID_EXPOSURE,
546 .type = V4L2_CTRL_TYPE_INTEGER,
547 .name = "Shutter Control",
548 .minimum = INDYCAM_SHUTTER_MIN,
549 .maximum = INDYCAM_SHUTTER_MAX,
551 .default_value = INDYCAM_SHUTTER_DEFAULT,
553 .id = V4L2_CID_GAMMA,
554 .type = V4L2_CTRL_TYPE_INTEGER,
556 .minimum = INDYCAM_GAMMA_MIN,
557 .maximum = INDYCAM_GAMMA_MAX,
559 .default_value = INDYCAM_GAMMA_DEFAULT,
563 #define VINO_SAA7191_V4L2_CONTROL_COUNT 9
565 struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
568 .type = V4L2_CTRL_TYPE_INTEGER,
570 .minimum = SAA7191_HUE_MIN,
571 .maximum = SAA7191_HUE_MAX,
573 .default_value = SAA7191_HUE_DEFAULT,
575 .id = SAA7191_CONTROL_BANDPASS,
576 .type = V4L2_CTRL_TYPE_INTEGER,
577 .name = "Luminance Bandpass",
578 .minimum = SAA7191_BANDPASS_MIN,
579 .maximum = SAA7191_BANDPASS_MAX,
581 .default_value = SAA7191_BANDPASS_DEFAULT,
583 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
584 .type = V4L2_CTRL_TYPE_INTEGER,
585 .name = "Luminance Bandpass Weight",
586 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
587 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
589 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
591 .id = SAA7191_CONTROL_CORING,
592 .type = V4L2_CTRL_TYPE_INTEGER,
593 .name = "HF Luminance Coring",
594 .minimum = SAA7191_CORING_MIN,
595 .maximum = SAA7191_CORING_MAX,
597 .default_value = SAA7191_CORING_DEFAULT,
599 .id = SAA7191_CONTROL_FORCE_COLOUR,
600 .type = V4L2_CTRL_TYPE_BOOLEAN,
601 .name = "Force Colour",
602 .minimum = SAA7191_FORCE_COLOUR_MIN,
603 .maximum = SAA7191_FORCE_COLOUR_MAX,
605 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
607 .id = SAA7191_CONTROL_CHROMA_GAIN,
608 .type = V4L2_CTRL_TYPE_INTEGER,
609 .name = "Chrominance Gain Control",
610 .minimum = SAA7191_CHROMA_GAIN_MIN,
611 .maximum = SAA7191_CHROMA_GAIN_MAX,
613 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
615 .id = SAA7191_CONTROL_VTRC,
616 .type = V4L2_CTRL_TYPE_BOOLEAN,
617 .name = "VTR Time Constant",
618 .minimum = SAA7191_VTRC_MIN,
619 .maximum = SAA7191_VTRC_MAX,
621 .default_value = SAA7191_VTRC_DEFAULT,
623 .id = SAA7191_CONTROL_LUMA_DELAY,
624 .type = V4L2_CTRL_TYPE_INTEGER,
625 .name = "Luminance Delay Compensation",
626 .minimum = SAA7191_LUMA_DELAY_MIN,
627 .maximum = SAA7191_LUMA_DELAY_MAX,
629 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
631 .id = SAA7191_CONTROL_VNR,
632 .type = V4L2_CTRL_TYPE_INTEGER,
633 .name = "Vertical Noise Reduction",
634 .minimum = SAA7191_VNR_MIN,
635 .maximum = SAA7191_VNR_MAX,
637 .default_value = SAA7191_VNR_DEFAULT,
641 /* VINO I2C bus functions */
643 unsigned i2c_vino_getctrl(void *data)
645 return vino->i2c_control;
648 void i2c_vino_setctrl(void *data, unsigned val)
650 vino->i2c_control = val;
653 unsigned i2c_vino_rdata(void *data)
655 return vino->i2c_data;
658 void i2c_vino_wdata(void *data, unsigned val)
660 vino->i2c_data = val;
663 static struct i2c_algo_sgi_data i2c_sgi_vino_data =
665 .getctrl = &i2c_vino_getctrl,
666 .setctrl = &i2c_vino_setctrl,
667 .rdata = &i2c_vino_rdata,
668 .wdata = &i2c_vino_wdata,
674 * There are two possible clients on VINO I2C bus, so we limit usage only
677 static int i2c_vino_client_reg(struct i2c_client *client)
682 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
683 switch (client->driver->id) {
684 case I2C_DRIVERID_SAA7191:
685 if (vino_drvdata->decoder.driver)
688 vino_drvdata->decoder.driver = client;
690 case I2C_DRIVERID_INDYCAM:
691 if (vino_drvdata->camera.driver)
694 vino_drvdata->camera.driver = client;
699 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
704 static int i2c_vino_client_unreg(struct i2c_client *client)
709 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
710 if (client == vino_drvdata->decoder.driver) {
711 if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
714 vino_drvdata->decoder.driver = NULL;
715 } else if (client == vino_drvdata->camera.driver) {
716 if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
719 vino_drvdata->camera.driver = NULL;
721 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
726 static struct i2c_adapter vino_i2c_adapter =
728 .name = "VINO I2C bus",
729 .id = I2C_HW_SGI_VINO,
730 .algo_data = &i2c_sgi_vino_data,
731 .client_register = &i2c_vino_client_reg,
732 .client_unregister = &i2c_vino_client_unreg,
735 static int vino_i2c_add_bus(void)
737 return i2c_sgi_add_bus(&vino_i2c_adapter);
740 static int vino_i2c_del_bus(void)
742 return i2c_del_adapter(&vino_i2c_adapter);
745 static int i2c_camera_command(unsigned int cmd, void *arg)
747 return vino_drvdata->camera.driver->
748 driver->command(vino_drvdata->camera.driver,
752 static int i2c_decoder_command(unsigned int cmd, void *arg)
754 return vino_drvdata->decoder.driver->
755 driver->command(vino_drvdata->decoder.driver,
759 /* VINO framebuffer/DMA descriptor management */
761 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
766 dprintk("vino_free_buffer_with_count(): count = %d\n", count);
768 for (i = 0; i < count; i++) {
769 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
770 dma_unmap_single(NULL,
771 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
772 PAGE_SIZE, DMA_FROM_DEVICE);
773 free_page(fb->desc_table.virtual[i]);
776 dma_free_coherent(NULL,
777 VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
778 sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
780 kfree(fb->desc_table.virtual);
782 memset(fb, 0, sizeof(struct vino_framebuffer));
785 static void vino_free_buffer(struct vino_framebuffer *fb)
787 vino_free_buffer_with_count(fb, fb->desc_table.page_count);
790 static int vino_allocate_buffer(struct vino_framebuffer *fb,
793 unsigned int count, i, j;
796 dprintk("vino_allocate_buffer():\n");
801 memset(fb, 0, sizeof(struct vino_framebuffer));
803 count = ((size / PAGE_SIZE) + 4) & ~3;
805 dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
808 /* allocate memory for table with virtual (page) addresses */
809 fb->desc_table.virtual = (unsigned long *)
810 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
811 if (!fb->desc_table.virtual)
814 /* allocate memory for table with dma addresses
815 * (has space for four extra descriptors) */
816 fb->desc_table.dma_cpu =
817 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
818 sizeof(dma_addr_t), &fb->desc_table.dma,
819 GFP_KERNEL | GFP_DMA);
820 if (!fb->desc_table.dma_cpu) {
822 goto out_free_virtual;
825 /* allocate pages for the buffer and acquire the according
827 for (i = 0; i < count; i++) {
828 dma_addr_t dma_data_addr;
830 fb->desc_table.virtual[i] =
831 get_zeroed_page(GFP_KERNEL | GFP_DMA);
832 if (!fb->desc_table.virtual[i]) {
839 (void *)fb->desc_table.virtual[i],
840 PAGE_SIZE, DMA_FROM_DEVICE);
842 for (j = 0; j < VINO_PAGE_RATIO; j++) {
843 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
844 dma_data_addr + VINO_PAGE_SIZE * j;
847 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
850 /* page_count needs to be set anyway, because the descriptor table has
851 * been allocated according to this number */
852 fb->desc_table.page_count = count;
855 /* the descriptor with index i doesn't contain
856 * a valid address yet */
857 vino_free_buffer_with_count(fb, i);
862 fb->size = count * PAGE_SIZE;
863 fb->data_format = VINO_DATA_FMT_NONE;
865 /* set the dma stop-bit for the last (count+1)th descriptor */
866 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
870 kfree(fb->desc_table.virtual);
875 /* user buffers not fully implemented yet */
876 static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
880 unsigned int count, i, j;
883 dprintk("vino_prepare_user_buffer():\n");
888 memset(fb, 0, sizeof(struct vino_framebuffer));
890 count = ((size / PAGE_SIZE)) & ~3;
892 dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
895 /* allocate memory for table with virtual (page) addresses */
896 fb->desc_table.virtual = (unsigned long *)
897 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
898 if (!fb->desc_table.virtual)
901 /* allocate memory for table with dma addresses
902 * (has space for four extra descriptors) */
903 fb->desc_table.dma_cpu =
904 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
905 sizeof(dma_addr_t), &fb->desc_table.dma,
906 GFP_KERNEL | GFP_DMA);
907 if (!fb->desc_table.dma_cpu) {
909 goto out_free_virtual;
912 /* allocate pages for the buffer and acquire the according
914 for (i = 0; i < count; i++) {
915 dma_addr_t dma_data_addr;
917 fb->desc_table.virtual[i] =
918 get_zeroed_page(GFP_KERNEL | GFP_DMA);
919 if (!fb->desc_table.virtual[i]) {
926 (void *)fb->desc_table.virtual[i],
927 PAGE_SIZE, DMA_FROM_DEVICE);
929 for (j = 0; j < VINO_PAGE_RATIO; j++) {
930 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
931 dma_data_addr + VINO_PAGE_SIZE * j;
934 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
937 /* page_count needs to be set anyway, because the descriptor table has
938 * been allocated according to this number */
939 fb->desc_table.page_count = count;
942 /* the descriptor with index i doesn't contain
943 * a valid address yet */
944 vino_free_buffer_with_count(fb, i);
949 fb->size = count * PAGE_SIZE;
951 /* set the dma stop-bit for the last (count+1)th descriptor */
952 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
956 kfree(fb->desc_table.virtual);
961 static void vino_sync_buffer(struct vino_framebuffer *fb)
965 dprintk("vino_sync_buffer():\n");
967 for (i = 0; i < fb->desc_table.page_count; i++)
968 dma_sync_single(NULL,
969 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
970 PAGE_SIZE, DMA_FROM_DEVICE);
973 /* Framebuffer fifo functions (need to be locked externally) */
975 static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
983 if (length > VINO_FRAMEBUFFER_COUNT_MAX)
984 length = VINO_FRAMEBUFFER_COUNT_MAX;
989 /* returns true/false */
990 static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
995 for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
996 if (f->data[i] == id)
1004 /* returns true/false */
1005 static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
1007 return (f->used == f->length);
1011 static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
1016 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
1018 if (id >= f->length) {
1019 return VINO_QUEUE_ERROR;
1022 if (vino_fifo_has_id(f, id)) {
1023 return VINO_QUEUE_ERROR;
1026 if (f->used < f->length) {
1027 f->data[f->tail] = id;
1028 f->tail = (f->tail + 1) % f->length;
1031 return VINO_QUEUE_ERROR;
1037 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
1040 *id = f->data[f->head];
1042 return VINO_QUEUE_ERROR;
1048 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
1051 *id = f->data[f->head];
1052 f->head = (f->head + 1) % f->length;
1055 return VINO_QUEUE_ERROR;
1061 /* Framebuffer queue functions */
1063 /* execute with queue_lock locked */
1064 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
1065 unsigned int length)
1070 memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
1071 memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
1072 for (i = 0; i < length; i++) {
1073 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
1075 vino_free_buffer(q->buffer[i]);
1076 kfree(q->buffer[i]);
1079 q->type = VINO_MEMORY_NONE;
1083 static void vino_queue_free(struct vino_framebuffer_queue *q)
1085 dprintk("vino_queue_free():\n");
1087 if (q->magic != VINO_QUEUE_MAGIC)
1089 if (q->type != VINO_MEMORY_MMAP)
1092 mutex_lock(&q->queue_mutex);
1094 vino_queue_free_with_count(q, q->length);
1096 mutex_unlock(&q->queue_mutex);
1099 static int vino_queue_init(struct vino_framebuffer_queue *q,
1100 unsigned int *length)
1105 dprintk("vino_queue_init(): length = %d\n", *length);
1107 if (q->magic == VINO_QUEUE_MAGIC) {
1108 dprintk("vino_queue_init(): queue already initialized!\n");
1112 if (q->type != VINO_MEMORY_NONE) {
1113 dprintk("vino_queue_init(): queue already initialized!\n");
1120 mutex_lock(&q->queue_mutex);
1122 if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1123 *length = VINO_FRAMEBUFFER_COUNT_MAX;
1127 for (i = 0; i < *length; i++) {
1128 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1129 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1131 if (!q->buffer[i]) {
1132 dprintk("vino_queue_init(): kmalloc() failed\n");
1137 ret = vino_allocate_buffer(q->buffer[i],
1138 VINO_FRAMEBUFFER_SIZE);
1140 kfree(q->buffer[i]);
1141 dprintk("vino_queue_init(): "
1142 "vino_allocate_buffer() failed\n");
1146 q->buffer[i]->id = i;
1148 q->buffer[i]->offset = q->buffer[i - 1]->offset +
1149 q->buffer[i - 1]->size;
1151 q->buffer[i]->offset = 0;
1154 spin_lock_init(&q->buffer[i]->state_lock);
1156 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1157 "size = %d\n", i, q->buffer[i]->offset,
1158 q->buffer[i]->size);
1162 vino_queue_free_with_count(q, i);
1165 q->length = *length;
1166 vino_fifo_init(&q->in, q->length);
1167 vino_fifo_init(&q->out, q->length);
1168 q->type = VINO_MEMORY_MMAP;
1169 q->magic = VINO_QUEUE_MAGIC;
1172 mutex_unlock(&q->queue_mutex);
1177 static struct vino_framebuffer *vino_queue_add(struct
1178 vino_framebuffer_queue *q,
1181 struct vino_framebuffer *ret = NULL;
1183 unsigned long flags;
1185 dprintk("vino_queue_add(): id = %d\n", id);
1187 if (q->magic != VINO_QUEUE_MAGIC) {
1191 spin_lock_irqsave(&q->queue_lock, flags);
1196 if (id >= q->length)
1199 /* not needed?: if (vino_fifo_full(&q->out)) {
1202 /* check that outgoing queue isn't already full
1203 * (or that it won't become full) */
1204 total = vino_fifo_get_used(&q->in) +
1205 vino_fifo_get_used(&q->out);
1206 if (total >= q->length)
1209 if (vino_fifo_enqueue(&q->in, id))
1212 ret = q->buffer[id];
1215 spin_unlock_irqrestore(&q->queue_lock, flags);
1220 static struct vino_framebuffer *vino_queue_transfer(struct
1221 vino_framebuffer_queue *q)
1223 struct vino_framebuffer *ret = NULL;
1224 struct vino_framebuffer *fb;
1226 unsigned long flags;
1228 dprintk("vino_queue_transfer():\n");
1230 if (q->magic != VINO_QUEUE_MAGIC) {
1234 spin_lock_irqsave(&q->queue_lock, flags);
1239 // now this actually removes an entry from the incoming queue
1240 if (vino_fifo_dequeue(&q->in, &id)) {
1244 dprintk("vino_queue_transfer(): id = %d\n", id);
1247 // we have already checked that the outgoing queue is not full, but...
1248 if (vino_fifo_enqueue(&q->out, id)) {
1249 printk(KERN_ERR "vino_queue_transfer(): "
1250 "outgoing queue is full, this shouldn't happen!\n");
1256 spin_unlock_irqrestore(&q->queue_lock, flags);
1261 /* returns true/false */
1262 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1266 unsigned long flags;
1268 if (q->magic != VINO_QUEUE_MAGIC) {
1272 spin_lock_irqsave(&q->queue_lock, flags);
1277 ret = vino_fifo_has_id(&q->in, id);
1280 spin_unlock_irqrestore(&q->queue_lock, flags);
1285 /* returns true/false */
1286 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1290 unsigned long flags;
1292 if (q->magic != VINO_QUEUE_MAGIC) {
1296 spin_lock_irqsave(&q->queue_lock, flags);
1301 ret = vino_fifo_has_id(&q->out, id);
1304 spin_unlock_irqrestore(&q->queue_lock, flags);
1309 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1313 unsigned long flags;
1315 if (q->magic != VINO_QUEUE_MAGIC) {
1316 return VINO_QUEUE_ERROR;
1319 spin_lock_irqsave(&q->queue_lock, flags);
1321 if (q->length == 0) {
1322 ret = VINO_QUEUE_ERROR;
1326 *used = vino_fifo_get_used(&q->in);
1329 spin_unlock_irqrestore(&q->queue_lock, flags);
1334 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1338 unsigned long flags;
1340 if (q->magic != VINO_QUEUE_MAGIC) {
1341 return VINO_QUEUE_ERROR;
1344 spin_lock_irqsave(&q->queue_lock, flags);
1346 if (q->length == 0) {
1347 ret = VINO_QUEUE_ERROR;
1351 *used = vino_fifo_get_used(&q->out);
1354 spin_unlock_irqrestore(&q->queue_lock, flags);
1360 static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1361 unsigned int *total)
1364 unsigned long flags;
1366 if (q->magic != VINO_QUEUE_MAGIC) {
1367 return VINO_QUEUE_ERROR;
1370 spin_lock_irqsave(&q->queue_lock, flags);
1372 if (q->length == 0) {
1373 ret = VINO_QUEUE_ERROR;
1377 *total = vino_fifo_get_used(&q->in) +
1378 vino_fifo_get_used(&q->out);
1381 spin_unlock_irqrestore(&q->queue_lock, flags);
1387 static struct vino_framebuffer *vino_queue_peek(struct
1388 vino_framebuffer_queue *q,
1391 struct vino_framebuffer *ret = NULL;
1392 unsigned long flags;
1394 if (q->magic != VINO_QUEUE_MAGIC) {
1398 spin_lock_irqsave(&q->queue_lock, flags);
1403 if (vino_fifo_peek(&q->in, id)) {
1407 ret = q->buffer[*id];
1409 spin_unlock_irqrestore(&q->queue_lock, flags);
1414 static struct vino_framebuffer *vino_queue_remove(struct
1415 vino_framebuffer_queue *q,
1418 struct vino_framebuffer *ret = NULL;
1419 unsigned long flags;
1420 dprintk("vino_queue_remove():\n");
1422 if (q->magic != VINO_QUEUE_MAGIC) {
1426 spin_lock_irqsave(&q->queue_lock, flags);
1431 if (vino_fifo_dequeue(&q->out, id)) {
1435 dprintk("vino_queue_remove(): id = %d\n", *id);
1436 ret = q->buffer[*id];
1438 spin_unlock_irqrestore(&q->queue_lock, flags);
1444 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1447 struct vino_framebuffer *ret = NULL;
1448 unsigned long flags;
1450 if (q->magic != VINO_QUEUE_MAGIC) {
1454 spin_lock_irqsave(&q->queue_lock, flags);
1459 if (id >= q->length)
1462 ret = q->buffer[id];
1464 spin_unlock_irqrestore(&q->queue_lock, flags);
1469 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1471 unsigned int length = 0;
1472 unsigned long flags;
1474 if (q->magic != VINO_QUEUE_MAGIC) {
1478 spin_lock_irqsave(&q->queue_lock, flags);
1480 spin_unlock_irqrestore(&q->queue_lock, flags);
1485 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1489 unsigned long flags;
1491 if (q->magic != VINO_QUEUE_MAGIC) {
1495 spin_lock_irqsave(&q->queue_lock, flags);
1496 for (i = 0; i < q->length; i++) {
1497 if (q->buffer[i]->map_count > 0) {
1502 spin_unlock_irqrestore(&q->queue_lock, flags);
1507 /* VINO functions */
1509 /* execute with input_lock locked */
1510 static void vino_update_line_size(struct vino_channel_settings *vcs)
1512 unsigned int w = vcs->clipping.right - vcs->clipping.left;
1513 unsigned int d = vcs->decimation;
1514 unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1517 dprintk("update_line_size(): before: w = %d, d = %d, "
1518 "line_size = %d\n", w, d, vcs->line_size);
1520 /* line size must be multiple of 8 bytes */
1521 lsize = (bpp * (w / d)) & ~7;
1522 w = (lsize / bpp) * d;
1524 vcs->clipping.right = vcs->clipping.left + w;
1525 vcs->line_size = lsize;
1527 dprintk("update_line_size(): after: w = %d, d = %d, "
1528 "line_size = %d\n", w, d, vcs->line_size);
1531 /* execute with input_lock locked */
1532 static void vino_set_clipping(struct vino_channel_settings *vcs,
1533 unsigned int x, unsigned int y,
1534 unsigned int w, unsigned int h)
1536 unsigned int maxwidth, maxheight;
1539 maxwidth = vino_data_norms[vcs->data_norm].width;
1540 maxheight = vino_data_norms[vcs->data_norm].height;
1541 d = vcs->decimation;
1543 y &= ~1; /* odd/even fields */
1548 if (y > maxheight) {
1552 if (((w / d) < VINO_MIN_WIDTH)
1553 || ((h / d) < VINO_MIN_HEIGHT)) {
1554 w = VINO_MIN_WIDTH * d;
1555 h = VINO_MIN_HEIGHT * d;
1558 if ((x + w) > maxwidth) {
1560 if ((w / d) < VINO_MIN_WIDTH)
1561 x = maxwidth - VINO_MIN_WIDTH * d;
1563 if ((y + h) > maxheight) {
1565 if ((h / d) < VINO_MIN_HEIGHT)
1566 y = maxheight - VINO_MIN_HEIGHT * d;
1569 vcs->clipping.left = x;
1570 vcs->clipping.top = y;
1571 vcs->clipping.right = x + w;
1572 vcs->clipping.bottom = y + h;
1574 vino_update_line_size(vcs);
1576 dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1577 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1578 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1581 /* execute with input_lock locked */
1582 static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
1584 vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1585 vino_data_norms[vcs->data_norm].height);
1588 /* execute with input_lock locked */
1589 static void vino_set_scaling(struct vino_channel_settings *vcs,
1590 unsigned int w, unsigned int h)
1592 unsigned int x, y, curw, curh, d;
1594 x = vcs->clipping.left;
1595 y = vcs->clipping.top;
1596 curw = vcs->clipping.right - vcs->clipping.left;
1597 curh = vcs->clipping.bottom - vcs->clipping.top;
1599 d = max(curw / w, curh / h);
1601 dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1602 w, h, curw, curh, d);
1610 vcs->decimation = d;
1611 vino_set_clipping(vcs, x, y, w * d, h * d);
1613 dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1614 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1615 vcs->decimation, vcs->line_size);
1618 /* execute with input_lock locked */
1619 static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
1621 vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1622 vcs->clipping.bottom - vcs->clipping.top);
1625 /* execute with input_lock locked */
1626 static void vino_set_framerate(struct vino_channel_settings *vcs,
1631 switch (vcs->data_norm) {
1632 case VINO_DATA_NORM_NTSC:
1633 case VINO_DATA_NORM_D1:
1634 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1636 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1637 fps = vino_data_norms[vcs->data_norm].fps_min;
1638 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1639 fps = vino_data_norms[vcs->data_norm].fps_max;
1658 mask = VINO_FRAMERT_FULL;
1660 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1662 case VINO_DATA_NORM_PAL:
1663 case VINO_DATA_NORM_SECAM:
1664 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1666 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1667 fps = vino_data_norms[vcs->data_norm].fps_min;
1668 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1669 fps = vino_data_norms[vcs->data_norm].fps_max;
1688 mask = VINO_FRAMERT_FULL;
1690 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1697 /* execute with input_lock locked */
1698 static inline void vino_set_default_framerate(struct
1699 vino_channel_settings *vcs)
1701 vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1705 * Prepare VINO for DMA transfer...
1706 * (execute only with vino_lock and input_lock locked)
1708 static int vino_dma_setup(struct vino_channel_settings *vcs,
1709 struct vino_framebuffer *fb)
1712 struct sgi_vino_channel *ch;
1713 const struct vino_data_norm *norm;
1715 dprintk("vino_dma_setup():\n");
1718 fb->frame_counter = 0;
1720 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1721 norm = &vino_data_norms[vcs->data_norm];
1726 /* VINO line size register is set 8 bytes less than actual */
1727 ch->line_size = vcs->line_size - 8;
1729 /* let VINO know where to transfer data */
1730 ch->start_desc_tbl = fb->desc_table.dma;
1731 ch->next_4_desc = fb->desc_table.dma;
1733 /* give vino time to fetch the first four descriptors, 5 usec
1734 * should be more than enough time */
1735 udelay(VINO_DESC_FETCH_DELAY);
1737 dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1738 ch->start_desc_tbl, ch->next_4_desc);
1740 /* set the alpha register */
1741 ch->alpha = vcs->alpha;
1743 /* set clipping registers */
1744 ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1745 VINO_CLIP_EVEN(norm->even.top +
1746 vcs->clipping.top / 2) |
1747 VINO_CLIP_X(vcs->clipping.left);
1748 ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1749 vcs->clipping.bottom / 2 - 1) |
1750 VINO_CLIP_EVEN(norm->even.top +
1751 vcs->clipping.bottom / 2 - 1) |
1752 VINO_CLIP_X(vcs->clipping.right);
1754 /* set the size of actual content in the buffer (DECIMATION !) */
1755 fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1757 ((vcs->clipping.bottom - vcs->clipping.top) /
1759 vino_data_formats[vcs->data_format].bpp;
1761 ch->frame_rate = vcs->framert_reg;
1763 ctrl = vino->control;
1764 intr = vino->intr_status;
1766 if (vcs->channel == VINO_CHANNEL_A) {
1767 /* All interrupt conditions for this channel was cleared
1768 * so clear the interrupt status register and enable
1770 intr &= ~VINO_INTSTAT_A;
1771 ctrl |= VINO_CTRL_A_INT;
1773 /* enable synchronization */
1774 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1776 /* enable frame assembly */
1777 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1779 /* set decimation used */
1780 if (vcs->decimation < 2)
1781 ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1783 ctrl |= VINO_CTRL_A_DEC_ENBL;
1784 ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1785 ctrl |= (vcs->decimation - 1) <<
1786 VINO_CTRL_A_DEC_SCALE_SHIFT;
1789 /* select input interface */
1790 if (vcs->input == VINO_INPUT_D1)
1791 ctrl |= VINO_CTRL_A_SELECT;
1793 ctrl &= ~VINO_CTRL_A_SELECT;
1796 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1797 VINO_CTRL_A_DITHER);
1799 intr &= ~VINO_INTSTAT_B;
1800 ctrl |= VINO_CTRL_B_INT;
1802 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1803 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1805 if (vcs->decimation < 2)
1806 ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1808 ctrl |= VINO_CTRL_B_DEC_ENBL;
1809 ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1810 ctrl |= (vcs->decimation - 1) <<
1811 VINO_CTRL_B_DEC_SCALE_SHIFT;
1814 if (vcs->input == VINO_INPUT_D1)
1815 ctrl |= VINO_CTRL_B_SELECT;
1817 ctrl &= ~VINO_CTRL_B_SELECT;
1819 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1820 VINO_CTRL_B_DITHER);
1824 fb->data_format = vcs->data_format;
1826 switch (vcs->data_format) {
1827 case VINO_DATA_FMT_GREY:
1828 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1829 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1831 case VINO_DATA_FMT_RGB32:
1832 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1833 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1835 case VINO_DATA_FMT_YUV:
1836 /* nothing needs to be done */
1838 case VINO_DATA_FMT_RGB332:
1839 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1840 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1841 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1845 vino->intr_status = intr;
1846 vino->control = ctrl;
1851 /* (execute only with vino_lock locked) */
1852 static inline void vino_dma_start(struct vino_channel_settings *vcs)
1854 u32 ctrl = vino->control;
1856 dprintk("vino_dma_start():\n");
1857 ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1858 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1859 vino->control = ctrl;
1862 /* (execute only with vino_lock locked) */
1863 static inline void vino_dma_stop(struct vino_channel_settings *vcs)
1865 u32 ctrl = vino->control;
1867 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1868 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1869 ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1870 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
1871 vino->control = ctrl;
1872 dprintk("vino_dma_stop():\n");
1876 * Load dummy page to descriptor registers. This prevents generating of
1877 * spurious interrupts. (execute only with vino_lock locked)
1879 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1881 struct sgi_vino_channel *ch;
1883 ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1888 ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1889 ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1891 udelay(VINO_DESC_FETCH_DELAY);
1892 dprintk("channel %c clear interrupt condition\n",
1893 (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1896 static int vino_capture(struct vino_channel_settings *vcs,
1897 struct vino_framebuffer *fb)
1900 unsigned long flags, flags2;
1902 spin_lock_irqsave(&fb->state_lock, flags);
1904 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1906 fb->state = VINO_FRAMEBUFFER_IN_USE;
1908 spin_unlock_irqrestore(&fb->state_lock, flags);
1913 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1914 spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1916 vino_dma_setup(vcs, fb);
1917 vino_dma_start(vcs);
1919 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
1920 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
1926 struct vino_framebuffer *vino_capture_enqueue(struct
1927 vino_channel_settings *vcs,
1930 struct vino_framebuffer *fb;
1931 unsigned long flags;
1933 dprintk("vino_capture_enqueue():\n");
1935 spin_lock_irqsave(&vcs->capture_lock, flags);
1937 fb = vino_queue_add(&vcs->fb_queue, index);
1939 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
1944 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1949 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
1951 struct vino_framebuffer *fb;
1952 unsigned int incoming, id;
1954 unsigned long flags;
1956 dprintk("vino_capture_next():\n");
1958 spin_lock_irqsave(&vcs->capture_lock, flags);
1961 /* start capture only if capture isn't in progress already */
1962 if (vcs->capturing) {
1963 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1968 /* capture next frame:
1969 * stop capture if capturing is not set */
1970 if (!vcs->capturing) {
1971 spin_unlock_irqrestore(&vcs->capture_lock, flags);
1976 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
1978 dprintk("vino_capture_next(): vino_queue_get_incoming() "
1983 if (incoming == 0) {
1984 dprintk("vino_capture_next(): no buffers available\n");
1988 fb = vino_queue_peek(&vcs->fb_queue, &id);
1990 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
1999 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2001 err = vino_capture(vcs, fb);
2007 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2012 static inline int vino_is_capturing(struct vino_channel_settings *vcs)
2015 unsigned long flags;
2017 spin_lock_irqsave(&vcs->capture_lock, flags);
2019 ret = vcs->capturing;
2021 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2026 /* waits until a frame is captured */
2027 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2032 dprintk("vino_wait_for_frame():\n");
2034 init_waitqueue_entry(&wait, current);
2035 /* add ourselves into wait queue */
2036 add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2038 /* to ensure that schedule_timeout will return immediately
2039 * if VINO interrupt was triggered meanwhile */
2040 schedule_timeout_interruptible(msecs_to_jiffies(100));
2042 if (signal_pending(current))
2045 remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2047 dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2048 err ? "failed" : "ok");
2053 /* the function assumes that PAGE_SIZE % 4 == 0 */
2054 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2055 unsigned char *pageptr;
2056 unsigned int page, i;
2059 for (page = 0; page < fb->desc_table.page_count; page++) {
2060 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2062 for (i = 0; i < PAGE_SIZE; i += 4) {
2064 pageptr[0] = pageptr[3];
2065 pageptr[1] = pageptr[2];
2066 pageptr[2] = pageptr[1];
2073 /* checks if the buffer is in correct state and syncs data */
2074 static int vino_check_buffer(struct vino_channel_settings *vcs,
2075 struct vino_framebuffer *fb)
2078 unsigned long flags;
2080 dprintk("vino_check_buffer():\n");
2082 spin_lock_irqsave(&fb->state_lock, flags);
2083 switch (fb->state) {
2084 case VINO_FRAMEBUFFER_IN_USE:
2087 case VINO_FRAMEBUFFER_READY:
2088 vino_sync_buffer(fb);
2089 fb->state = VINO_FRAMEBUFFER_UNUSED;
2094 spin_unlock_irqrestore(&fb->state_lock, flags);
2097 if (vino_pixel_conversion
2098 && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2099 vino_convert_to_rgba(fb);
2101 } else if (err && (err != -EINVAL)) {
2102 dprintk("vino_check_buffer(): buffer not ready\n");
2104 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2106 vino_clear_interrupt(vcs);
2107 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2113 /* forcefully terminates capture */
2114 static void vino_capture_stop(struct vino_channel_settings *vcs)
2116 unsigned int incoming = 0, outgoing = 0, id;
2117 unsigned long flags, flags2;
2119 dprintk("vino_capture_stop():\n");
2121 spin_lock_irqsave(&vcs->capture_lock, flags);
2123 /* unset capturing to stop queue processing */
2126 spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2129 vino_clear_interrupt(vcs);
2131 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2133 /* remove all items from the queue */
2134 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2135 dprintk("vino_capture_stop(): "
2136 "vino_queue_get_incoming() failed\n");
2139 while (incoming > 0) {
2140 vino_queue_transfer(&vcs->fb_queue);
2142 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2143 dprintk("vino_capture_stop(): "
2144 "vino_queue_get_incoming() failed\n");
2149 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2150 dprintk("vino_capture_stop(): "
2151 "vino_queue_get_outgoing() failed\n");
2154 while (outgoing > 0) {
2155 vino_queue_remove(&vcs->fb_queue, &id);
2157 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2158 dprintk("vino_capture_stop(): "
2159 "vino_queue_get_outgoing() failed\n");
2165 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2169 static int vino_capture_failed(struct vino_channel_settings *vcs)
2171 struct vino_framebuffer *fb;
2172 unsigned long flags;
2176 dprintk("vino_capture_failed():\n");
2178 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2181 vino_clear_interrupt(vcs);
2183 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2185 ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2186 if (ret == VINO_QUEUE_ERROR) {
2187 dprintk("vino_queue_get_incoming() failed\n");
2191 /* no buffers to process */
2195 fb = vino_queue_peek(&vcs->fb_queue, &i);
2197 dprintk("vino_queue_peek() failed\n");
2201 spin_lock_irqsave(&fb->state_lock, flags);
2202 if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2203 fb->state = VINO_FRAMEBUFFER_UNUSED;
2204 vino_queue_transfer(&vcs->fb_queue);
2205 vino_queue_remove(&vcs->fb_queue, &i);
2206 /* we should actually discard the newest frame,
2207 * but who cares ... */
2209 spin_unlock_irqrestore(&fb->state_lock, flags);
2215 static void vino_skip_frame(struct vino_channel_settings *vcs)
2217 struct vino_framebuffer *fb;
2218 unsigned long flags;
2221 spin_lock_irqsave(&vcs->capture_lock, flags);
2222 fb = vino_queue_peek(&vcs->fb_queue, &id);
2224 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2225 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2228 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2230 spin_lock_irqsave(&fb->state_lock, flags);
2231 fb->state = VINO_FRAMEBUFFER_UNUSED;
2232 spin_unlock_irqrestore(&fb->state_lock, flags);
2234 vino_capture_next(vcs, 0);
2237 static void vino_frame_done(struct vino_channel_settings *vcs)
2239 struct vino_framebuffer *fb;
2240 unsigned long flags;
2242 spin_lock_irqsave(&vcs->capture_lock, flags);
2243 fb = vino_queue_transfer(&vcs->fb_queue);
2245 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2246 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2249 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2251 fb->frame_counter = vcs->int_data.frame_counter;
2252 memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2253 sizeof(struct timeval));
2255 spin_lock_irqsave(&fb->state_lock, flags);
2256 if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2257 fb->state = VINO_FRAMEBUFFER_READY;
2258 spin_unlock_irqrestore(&fb->state_lock, flags);
2260 wake_up(&vcs->fb_queue.frame_wait_queue);
2262 vino_capture_next(vcs, 0);
2265 static void vino_capture_tasklet(unsigned long channel) {
2266 struct vino_channel_settings *vcs;
2268 vcs = (channel == VINO_CHANNEL_A)
2269 ? &vino_drvdata->a : &vino_drvdata->b;
2271 if (vcs->int_data.skip)
2272 vcs->int_data.skip_count++;
2274 if (vcs->int_data.skip && (vcs->int_data.skip_count
2275 <= VINO_MAX_FRAME_SKIP_COUNT)) {
2276 vino_skip_frame(vcs);
2278 vcs->int_data.skip_count = 0;
2279 vino_frame_done(vcs);
2283 static irqreturn_t vino_interrupt(int irq, void *dev_id)
2286 unsigned int fc_a, fc_b;
2287 int handled_a = 0, skip_a = 0, done_a = 0;
2288 int handled_b = 0, skip_b = 0, done_b = 0;
2290 #ifdef VINO_DEBUG_INT
2292 unsigned int line_count = vino->a.line_count,
2293 page_index = vino->a.page_index,
2294 field_counter = vino->a.field_counter,
2295 start_desc_tbl = vino->a.start_desc_tbl,
2296 next_4_desc = vino->a.next_4_desc;
2297 unsigned int line_count_2,
2304 spin_lock(&vino_drvdata->vino_lock);
2306 while ((intr = vino->intr_status)) {
2307 fc_a = vino->a.field_counter >> 1;
2308 fc_b = vino->b.field_counter >> 1;
2310 /* handle error-interrupts in some special way ?
2311 * --> skips frames */
2312 if (intr & VINO_INTSTAT_A) {
2313 if (intr & VINO_INTSTAT_A_EOF) {
2314 vino_drvdata->a.field++;
2315 if (vino_drvdata->a.field > 1) {
2316 vino_dma_stop(&vino_drvdata->a);
2317 vino_clear_interrupt(&vino_drvdata->a);
2318 vino_drvdata->a.field = 0;
2321 if (vino->a.page_index
2322 != vino_drvdata->a.line_size) {
2323 vino->a.line_count = 0;
2324 vino->a.page_index =
2327 vino->a.next_4_desc =
2328 vino->a.start_desc_tbl;
2331 dprintk("channel A end-of-field "
2332 "interrupt: %04x\n", intr);
2334 vino_dma_stop(&vino_drvdata->a);
2335 vino_clear_interrupt(&vino_drvdata->a);
2336 vino_drvdata->a.field = 0;
2338 dprintk("channel A error interrupt: %04x\n",
2342 #ifdef VINO_DEBUG_INT
2343 line_count_2 = vino->a.line_count;
2344 page_index_2 = vino->a.page_index;
2345 field_counter_2 = vino->a.field_counter;
2346 start_desc_tbl_2 = vino->a.start_desc_tbl;
2347 next_4_desc_2 = vino->a.next_4_desc;
2349 printk("intr = %04x, loop = %d, field = %d\n",
2350 intr, loop, vino_drvdata->a.field);
2351 printk("1- line count = %04d, page index = %04d, "
2352 "start = %08x, next = %08x\n"
2353 " fieldc = %d, framec = %d\n",
2354 line_count, page_index, start_desc_tbl,
2355 next_4_desc, field_counter, fc_a);
2356 printk("12-line count = %04d, page index = %04d, "
2357 " start = %08x, next = %08x\n",
2358 line_count_2, page_index_2, start_desc_tbl_2,
2366 if (intr & VINO_INTSTAT_B) {
2367 if (intr & VINO_INTSTAT_B_EOF) {
2368 vino_drvdata->b.field++;
2369 if (vino_drvdata->b.field > 1) {
2370 vino_dma_stop(&vino_drvdata->b);
2371 vino_clear_interrupt(&vino_drvdata->b);
2372 vino_drvdata->b.field = 0;
2375 dprintk("channel B end-of-field "
2376 "interrupt: %04x\n", intr);
2378 vino_dma_stop(&vino_drvdata->b);
2379 vino_clear_interrupt(&vino_drvdata->b);
2380 vino_drvdata->b.field = 0;
2382 dprintk("channel B error interrupt: %04x\n",
2387 /* Always remember to clear interrupt status.
2388 * Disable VINO interrupts while we do this. */
2389 ctrl = vino->control;
2390 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2391 vino->intr_status = ~intr;
2392 vino->control = ctrl;
2394 spin_unlock(&vino_drvdata->vino_lock);
2396 if ((!handled_a) && (done_a || skip_a)) {
2398 do_gettimeofday(&vino_drvdata->
2399 a.int_data.timestamp);
2400 vino_drvdata->a.int_data.frame_counter = fc_a;
2402 vino_drvdata->a.int_data.skip = skip_a;
2404 dprintk("channel A %s, interrupt: %d\n",
2405 skip_a ? "skipping frame" : "frame done",
2407 tasklet_hi_schedule(&vino_tasklet_a);
2411 if ((!handled_b) && (done_b || skip_b)) {
2413 do_gettimeofday(&vino_drvdata->
2414 b.int_data.timestamp);
2415 vino_drvdata->b.int_data.frame_counter = fc_b;
2417 vino_drvdata->b.int_data.skip = skip_b;
2419 dprintk("channel B %s, interrupt: %d\n",
2420 skip_b ? "skipping frame" : "frame done",
2422 tasklet_hi_schedule(&vino_tasklet_b);
2426 #ifdef VINO_DEBUG_INT
2429 spin_lock(&vino_drvdata->vino_lock);
2432 spin_unlock(&vino_drvdata->vino_lock);
2437 /* VINO video input management */
2439 static int vino_get_saa7191_input(int input)
2442 case VINO_INPUT_COMPOSITE:
2443 return SAA7191_INPUT_COMPOSITE;
2444 case VINO_INPUT_SVIDEO:
2445 return SAA7191_INPUT_SVIDEO;
2447 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2448 "invalid input!\n");
2453 /* execute with input_lock locked */
2454 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2456 switch(vcs->input) {
2457 case VINO_INPUT_COMPOSITE:
2458 case VINO_INPUT_SVIDEO:
2459 return (vino_drvdata->decoder.owner == vcs->channel);
2461 return (vino_drvdata->camera.owner == vcs->channel);
2467 static int vino_acquire_input(struct vino_channel_settings *vcs)
2469 unsigned long flags;
2472 dprintk("vino_acquire_input():\n");
2474 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2476 /* First try D1 and then SAA7191 */
2477 if (vino_drvdata->camera.driver
2478 && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
2479 i2c_use_client(vino_drvdata->camera.driver);
2480 vino_drvdata->camera.owner = vcs->channel;
2481 vcs->input = VINO_INPUT_D1;
2482 vcs->data_norm = VINO_DATA_NORM_D1;
2483 } else if (vino_drvdata->decoder.driver
2484 && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
2488 struct v4l2_routing route = { 0, 0 };
2490 i2c_use_client(vino_drvdata->decoder.driver);
2491 input = VINO_INPUT_COMPOSITE;
2493 route.input = vino_get_saa7191_input(input);
2494 ret = i2c_decoder_command(VIDIOC_INT_S_VIDEO_ROUTING, &route);
2500 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2502 /* Don't hold spinlocks while auto-detecting norm
2503 * as it may take a while... */
2505 ret = i2c_decoder_command(VIDIOC_QUERYSTD, &norm);
2507 for (data_norm = 0; data_norm < 3; data_norm++) {
2508 if (vino_data_norms[data_norm].std & norm)
2512 data_norm = VINO_DATA_NORM_PAL;
2513 ret = i2c_decoder_command(VIDIOC_S_STD, &norm);
2516 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2523 vino_drvdata->decoder.owner = vcs->channel;
2526 vcs->data_norm = data_norm;
2528 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2529 vino_drvdata->b.input : vino_drvdata->a.input;
2530 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2531 vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2534 if (vcs->input == VINO_INPUT_NONE) {
2539 vino_set_default_clipping(vcs);
2540 vino_set_default_scaling(vcs);
2541 vino_set_default_framerate(vcs);
2543 dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2546 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2551 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2553 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2554 &vino_drvdata->b : &vino_drvdata->a;
2555 unsigned long flags;
2558 dprintk("vino_set_input():\n");
2560 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2562 if (vcs->input == input)
2566 case VINO_INPUT_COMPOSITE:
2567 case VINO_INPUT_SVIDEO:
2568 if (!vino_drvdata->decoder.driver) {
2573 if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
2574 i2c_use_client(vino_drvdata->decoder.driver);
2575 vino_drvdata->decoder.owner = vcs->channel;
2578 if (vino_drvdata->decoder.owner == vcs->channel) {
2581 struct v4l2_routing route = { 0, 0 };
2583 route.input = vino_get_saa7191_input(input);
2584 ret = i2c_decoder_command(VIDIOC_INT_S_VIDEO_ROUTING, &route);
2586 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2591 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2593 /* Don't hold spinlocks while auto-detecting norm
2594 * as it may take a while... */
2596 ret = i2c_decoder_command(VIDIOC_QUERYSTD, &norm);
2598 for (data_norm = 0; data_norm < 3; data_norm++) {
2599 if (vino_data_norms[data_norm].std & norm)
2603 data_norm = VINO_DATA_NORM_PAL;
2604 ret = i2c_decoder_command(VIDIOC_S_STD, &norm);
2607 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2610 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2616 vcs->data_norm = data_norm;
2618 if (input != vcs2->input) {
2624 vcs->data_norm = vcs2->data_norm;
2627 if (vino_drvdata->camera.owner == vcs->channel) {
2628 /* Transfer the ownership or release the input */
2629 if (vcs2->input == VINO_INPUT_D1) {
2630 vino_drvdata->camera.owner = vcs2->channel;
2632 i2c_release_client(vino_drvdata->camera.driver);
2633 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2638 if (!vino_drvdata->camera.driver) {
2643 if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
2644 i2c_use_client(vino_drvdata->camera.driver);
2645 vino_drvdata->camera.owner = vcs->channel;
2648 if (vino_drvdata->decoder.owner == vcs->channel) {
2649 /* Transfer the ownership or release the input */
2650 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2651 (vcs2->input == VINO_INPUT_SVIDEO)) {
2652 vino_drvdata->decoder.owner = vcs2->channel;
2654 i2c_release_client(vino_drvdata->
2656 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2661 vcs->data_norm = VINO_DATA_NORM_D1;
2668 vino_set_default_clipping(vcs);
2669 vino_set_default_scaling(vcs);
2670 vino_set_default_framerate(vcs);
2672 dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2675 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2680 static void vino_release_input(struct vino_channel_settings *vcs)
2682 struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2683 &vino_drvdata->b : &vino_drvdata->a;
2684 unsigned long flags;
2686 dprintk("vino_release_input():\n");
2688 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2690 /* Release ownership of the channel
2691 * and if the other channel takes input from
2692 * the same source, transfer the ownership */
2693 if (vino_drvdata->camera.owner == vcs->channel) {
2694 if (vcs2->input == VINO_INPUT_D1) {
2695 vino_drvdata->camera.owner = vcs2->channel;
2697 i2c_release_client(vino_drvdata->camera.driver);
2698 vino_drvdata->camera.owner = VINO_NO_CHANNEL;
2700 } else if (vino_drvdata->decoder.owner == vcs->channel) {
2701 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2702 (vcs2->input == VINO_INPUT_SVIDEO)) {
2703 vino_drvdata->decoder.owner = vcs2->channel;
2705 i2c_release_client(vino_drvdata->decoder.driver);
2706 vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
2709 vcs->input = VINO_INPUT_NONE;
2711 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2714 /* execute with input_lock locked */
2715 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2716 unsigned int data_norm,
2717 unsigned long *flags)
2721 if (data_norm == vcs->data_norm)
2724 switch (vcs->input) {
2726 /* only one "norm" supported */
2727 if (data_norm != VINO_DATA_NORM_D1)
2730 case VINO_INPUT_COMPOSITE:
2731 case VINO_INPUT_SVIDEO: {
2734 if ((data_norm != VINO_DATA_NORM_PAL)
2735 && (data_norm != VINO_DATA_NORM_NTSC)
2736 && (data_norm != VINO_DATA_NORM_SECAM))
2739 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
2741 /* Don't hold spinlocks while setting norm
2742 * as it may take a while... */
2744 norm = vino_data_norms[data_norm].std;
2745 err = i2c_decoder_command(VIDIOC_S_STD, &norm);
2747 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2752 vcs->data_norm = data_norm;
2754 vino_set_default_clipping(vcs);
2755 vino_set_default_scaling(vcs);
2756 vino_set_default_framerate(vcs);
2767 /* V4L2 helper functions */
2769 static int vino_find_data_format(__u32 pixelformat)
2773 for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2774 if (vino_data_formats[i].pixelformat == pixelformat)
2778 return VINO_DATA_FMT_NONE;
2781 static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
2783 int input = VINO_INPUT_NONE;
2784 unsigned long flags;
2786 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2787 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2790 input = VINO_INPUT_COMPOSITE;
2793 input = VINO_INPUT_SVIDEO;
2796 input = VINO_INPUT_D1;
2799 } else if (vino_drvdata->decoder.driver) {
2802 input = VINO_INPUT_COMPOSITE;
2805 input = VINO_INPUT_SVIDEO;
2808 } else if (vino_drvdata->camera.driver) {
2811 input = VINO_INPUT_D1;
2815 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2820 /* execute with input_lock locked */
2821 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2824 // FIXME: detect when no inputs available
2826 if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
2827 switch (vcs->input) {
2828 case VINO_INPUT_COMPOSITE:
2831 case VINO_INPUT_SVIDEO:
2838 } else if (vino_drvdata->decoder.driver) {
2839 switch (vcs->input) {
2840 case VINO_INPUT_COMPOSITE:
2843 case VINO_INPUT_SVIDEO:
2847 } else if (vino_drvdata->camera.driver) {
2848 switch (vcs->input) {
2860 static int vino_querycap(struct file *file, void *__fh,
2861 struct v4l2_capability *cap)
2863 memset(cap, 0, sizeof(struct v4l2_capability));
2865 strcpy(cap->driver, vino_driver_name);
2866 strcpy(cap->card, vino_driver_description);
2867 strcpy(cap->bus_info, vino_bus_name);
2868 cap->version = VINO_VERSION_CODE;
2870 V4L2_CAP_VIDEO_CAPTURE |
2872 // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2876 static int vino_enum_input(struct file *file, void *__fh,
2877 struct v4l2_input *i)
2879 struct vino_channel_settings *vcs = video_drvdata(file);
2880 __u32 index = i->index;
2882 dprintk("requested index = %d\n", index);
2884 input = vino_int_enum_input(vcs, index);
2885 if (input == VINO_INPUT_NONE)
2888 memset(i, 0, sizeof(struct v4l2_input));
2891 i->type = V4L2_INPUT_TYPE_CAMERA;
2892 i->std = vino_inputs[input].std;
2893 strcpy(i->name, vino_inputs[input].name);
2895 if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2896 i2c_decoder_command(VIDIOC_INT_G_INPUT_STATUS, &i->status);
2900 static int vino_g_input(struct file *file, void *__fh,
2903 struct vino_channel_settings *vcs = video_drvdata(file);
2906 unsigned long flags;
2908 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2910 index = vino_find_input_index(vcs);
2911 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2913 dprintk("input = %d\n", input);
2915 if (input == VINO_INPUT_NONE) {
2924 static int vino_s_input(struct file *file, void *__fh,
2927 struct vino_channel_settings *vcs = video_drvdata(file);
2929 dprintk("requested input = %d\n", i);
2931 input = vino_int_enum_input(vcs, i);
2932 if (input == VINO_INPUT_NONE)
2935 return vino_set_input(vcs, input);
2938 static int vino_querystd(struct file *file, void *__fh,
2941 struct vino_channel_settings *vcs = video_drvdata(file);
2942 unsigned long flags;
2945 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2947 switch (vcs->input) {
2949 *std = vino_inputs[vcs->input].std;
2951 case VINO_INPUT_COMPOSITE:
2952 case VINO_INPUT_SVIDEO: {
2953 i2c_decoder_command(VIDIOC_QUERYSTD, std);
2960 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2965 static int vino_g_std(struct file *file, void *__fh,
2968 struct vino_channel_settings *vcs = video_drvdata(file);
2969 unsigned long flags;
2971 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2973 *std = vino_data_norms[vcs->data_norm].std;
2974 dprintk("current standard = %d\n", vcs->data_norm);
2976 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2981 static int vino_s_std(struct file *file, void *__fh,
2984 struct vino_channel_settings *vcs = video_drvdata(file);
2985 unsigned long flags;
2988 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2990 if (!vino_is_input_owner(vcs)) {
2995 /* check if the standard is valid for the current input */
2996 if ((*std) & vino_inputs[vcs->input].std) {
2997 dprintk("standard accepted\n");
2999 /* change the video norm for SAA7191
3000 * and accept NTSC for D1 (do nothing) */
3002 if (vcs->input == VINO_INPUT_D1)
3005 if ((*std) & V4L2_STD_PAL) {
3006 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3008 } else if ((*std) & V4L2_STD_NTSC) {
3009 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3011 } else if ((*std) & V4L2_STD_SECAM) {
3012 ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3026 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3031 static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
3032 struct v4l2_fmtdesc *fd)
3034 enum v4l2_buf_type type = fd->type;
3035 int index = fd->index;
3037 dprintk("format index = %d\n", index);
3039 if ((fd->index < 0) ||
3040 (fd->index >= VINO_DATA_FMT_COUNT))
3042 dprintk("format name = %s\n",
3043 vino_data_formats[index].description);
3045 memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3048 fd->pixelformat = vino_data_formats[index].pixelformat;
3049 strcpy(fd->description, vino_data_formats[index].description);
3053 static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
3054 struct v4l2_format *f)
3056 struct vino_channel_settings *vcs = video_drvdata(file);
3057 struct vino_channel_settings tempvcs;
3058 unsigned long flags;
3059 struct v4l2_pix_format *pf = &f->fmt.pix;
3061 dprintk("requested: w = %d, h = %d\n",
3062 pf->width, pf->height);
3064 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3065 memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3066 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3068 tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3069 if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3070 tempvcs.data_format = VINO_DATA_FMT_GREY;
3072 vino_data_formats[tempvcs.data_format].
3076 /* data format must be set before clipping/scaling */
3077 vino_set_scaling(&tempvcs, pf->width, pf->height);
3079 dprintk("data format = %s\n",
3080 vino_data_formats[tempvcs.data_format].description);
3082 pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3084 pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3087 pf->field = V4L2_FIELD_INTERLACED;
3088 pf->bytesperline = tempvcs.line_size;
3089 pf->sizeimage = tempvcs.line_size *
3090 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3093 vino_data_formats[tempvcs.data_format].colorspace;
3099 static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
3100 struct v4l2_format *f)
3102 struct vino_channel_settings *vcs = video_drvdata(file);
3103 unsigned long flags;
3104 struct v4l2_pix_format *pf = &f->fmt.pix;
3106 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3108 pf->width = (vcs->clipping.right - vcs->clipping.left) /
3110 pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3113 vino_data_formats[vcs->data_format].pixelformat;
3115 pf->field = V4L2_FIELD_INTERLACED;
3116 pf->bytesperline = vcs->line_size;
3117 pf->sizeimage = vcs->line_size *
3118 (vcs->clipping.bottom - vcs->clipping.top) /
3121 vino_data_formats[vcs->data_format].colorspace;
3125 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3129 static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3130 struct v4l2_format *f)
3132 struct vino_channel_settings *vcs = video_drvdata(file);
3134 unsigned long flags;
3135 struct v4l2_pix_format *pf = &f->fmt.pix;
3137 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3139 data_format = vino_find_data_format(pf->pixelformat);
3141 if (data_format == VINO_DATA_FMT_NONE) {
3142 vcs->data_format = VINO_DATA_FMT_GREY;
3144 vino_data_formats[vcs->data_format].
3147 vcs->data_format = data_format;
3150 /* data format must be set before clipping/scaling */
3151 vino_set_scaling(vcs, pf->width, pf->height);
3153 dprintk("data format = %s\n",
3154 vino_data_formats[vcs->data_format].description);
3156 pf->width = vcs->clipping.right - vcs->clipping.left;
3157 pf->height = vcs->clipping.bottom - vcs->clipping.top;
3159 pf->field = V4L2_FIELD_INTERLACED;
3160 pf->bytesperline = vcs->line_size;
3161 pf->sizeimage = vcs->line_size *
3162 (vcs->clipping.bottom - vcs->clipping.top) /
3165 vino_data_formats[vcs->data_format].colorspace;
3169 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3173 static int vino_cropcap(struct file *file, void *__fh,
3174 struct v4l2_cropcap *ccap)
3176 struct vino_channel_settings *vcs = video_drvdata(file);
3177 const struct vino_data_norm *norm;
3178 unsigned long flags;
3180 switch (ccap->type) {
3181 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3182 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3184 norm = &vino_data_norms[vcs->data_norm];
3186 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3188 ccap->bounds.left = 0;
3189 ccap->bounds.top = 0;
3190 ccap->bounds.width = norm->width;
3191 ccap->bounds.height = norm->height;
3192 memcpy(&ccap->defrect, &ccap->bounds,
3193 sizeof(struct v4l2_rect));
3195 ccap->pixelaspect.numerator = 1;
3196 ccap->pixelaspect.denominator = 1;
3198 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3206 static int vino_g_crop(struct file *file, void *__fh,
3207 struct v4l2_crop *c)
3209 struct vino_channel_settings *vcs = video_drvdata(file);
3210 unsigned long flags;
3213 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3214 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3216 c->c.left = vcs->clipping.left;
3217 c->c.top = vcs->clipping.top;
3218 c->c.width = vcs->clipping.right - vcs->clipping.left;
3219 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3221 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3223 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3231 static int vino_s_crop(struct file *file, void *__fh,
3232 struct v4l2_crop *c)
3234 struct vino_channel_settings *vcs = video_drvdata(file);
3235 unsigned long flags;
3238 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3239 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3241 vino_set_clipping(vcs, c->c.left, c->c.top,
3242 c->c.width, c->c.height);
3244 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3246 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3254 static int vino_g_parm(struct file *file, void *__fh,
3255 struct v4l2_streamparm *sp)
3257 struct vino_channel_settings *vcs = video_drvdata(file);
3258 unsigned long flags;
3261 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3262 struct v4l2_captureparm *cp = &sp->parm.capture;
3263 memset(cp, 0, sizeof(struct v4l2_captureparm));
3265 cp->capability = V4L2_CAP_TIMEPERFRAME;
3266 cp->timeperframe.numerator = 1;
3268 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3270 cp->timeperframe.denominator = vcs->fps;
3272 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3274 // TODO: cp->readbuffers = xxx;
3277 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3285 static int vino_s_parm(struct file *file, void *__fh,
3286 struct v4l2_streamparm *sp)
3288 struct vino_channel_settings *vcs = video_drvdata(file);
3289 unsigned long flags;
3292 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3293 struct v4l2_captureparm *cp = &sp->parm.capture;
3295 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3297 if ((cp->timeperframe.numerator == 0) ||
3298 (cp->timeperframe.denominator == 0)) {
3299 /* reset framerate */
3300 vino_set_default_framerate(vcs);
3302 vino_set_framerate(vcs, cp->timeperframe.denominator /
3303 cp->timeperframe.numerator);
3306 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3308 // TODO: set buffers according to cp->readbuffers
3311 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3319 static int vino_reqbufs(struct file *file, void *__fh,
3320 struct v4l2_requestbuffers *rb)
3322 struct vino_channel_settings *vcs = video_drvdata(file);
3327 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3328 // TODO: check queue type
3329 if (rb->memory != V4L2_MEMORY_MMAP) {
3330 dprintk("type not mmap\n");
3334 dprintk("count = %d\n", rb->count);
3335 if (rb->count > 0) {
3336 if (vino_is_capturing(vcs)) {
3337 dprintk("busy, capturing\n");
3341 if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3342 dprintk("busy, buffers still mapped\n");
3346 vino_queue_free(&vcs->fb_queue);
3347 vino_queue_init(&vcs->fb_queue, &rb->count);
3351 vino_capture_stop(vcs);
3352 vino_queue_free(&vcs->fb_queue);
3356 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3364 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3365 struct vino_framebuffer *fb,
3366 struct v4l2_buffer *b)
3368 if (vino_queue_outgoing_contains(&vcs->fb_queue,
3370 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3371 b->flags |= V4L2_BUF_FLAG_DONE;
3372 } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3374 b->flags &= ~V4L2_BUF_FLAG_DONE;
3375 b->flags |= V4L2_BUF_FLAG_QUEUED;
3377 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3378 V4L2_BUF_FLAG_QUEUED);
3381 b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3383 if (fb->map_count > 0)
3384 b->flags |= V4L2_BUF_FLAG_MAPPED;
3387 b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3388 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3389 b->m.offset = fb->offset;
3390 b->bytesused = fb->data_size;
3391 b->length = fb->size;
3392 b->field = V4L2_FIELD_INTERLACED;
3393 b->sequence = fb->frame_counter;
3394 memcpy(&b->timestamp, &fb->timestamp,
3395 sizeof(struct timeval));
3398 dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3399 fb->id, fb->size, fb->data_size, fb->offset);
3402 static int vino_querybuf(struct file *file, void *__fh,
3403 struct v4l2_buffer *b)
3405 struct vino_channel_settings *vcs = video_drvdata(file);
3410 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3411 struct vino_framebuffer *fb;
3413 // TODO: check queue type
3414 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3415 dprintk("invalid index = %d\n",
3420 fb = vino_queue_get_buffer(&vcs->fb_queue,
3423 dprintk("vino_queue_get_buffer() failed");
3427 vino_v4l2_get_buffer_status(vcs, fb, b);
3430 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3438 static int vino_qbuf(struct file *file, void *__fh,
3439 struct v4l2_buffer *b)
3441 struct vino_channel_settings *vcs = video_drvdata(file);
3446 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3447 struct vino_framebuffer *fb;
3450 // TODO: check queue type
3451 if (b->memory != V4L2_MEMORY_MMAP) {
3452 dprintk("type not mmap\n");
3456 fb = vino_capture_enqueue(vcs, b->index);
3460 vino_v4l2_get_buffer_status(vcs, fb, b);
3462 if (vcs->streaming) {
3463 ret = vino_capture_next(vcs, 1);
3469 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3477 static int vino_dqbuf(struct file *file, void *__fh,
3478 struct v4l2_buffer *b)
3480 struct vino_channel_settings *vcs = video_drvdata(file);
3481 unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3486 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3487 struct vino_framebuffer *fb;
3488 unsigned int incoming, outgoing;
3491 // TODO: check queue type
3493 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3495 dprintk("vino_queue_get_incoming() failed\n");
3498 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3500 dprintk("vino_queue_get_outgoing() failed\n");
3504 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3506 if (outgoing == 0) {
3507 if (incoming == 0) {
3508 dprintk("no incoming or outgoing buffers\n");
3512 dprintk("non-blocking I/O was selected and "
3513 "there are no buffers to dequeue\n");
3517 err = vino_wait_for_frame(vcs);
3519 err = vino_wait_for_frame(vcs);
3522 * no frames captured because
3523 * of frame skipping */
3524 // vino_capture_failed(vcs);
3530 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3532 dprintk("vino_queue_remove() failed\n");
3536 err = vino_check_buffer(vcs, fb);
3538 vino_v4l2_get_buffer_status(vcs, fb, b);
3545 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3553 static int vino_streamon(struct file *file, void *__fh,
3554 enum v4l2_buf_type i)
3556 struct vino_channel_settings *vcs = video_drvdata(file);
3557 unsigned int incoming;
3565 // TODO: check queue type
3567 if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3568 dprintk("no buffers allocated\n");
3572 ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3574 dprintk("vino_queue_get_incoming() failed\n");
3581 ret = vino_capture_next(vcs, 1);
3585 dprintk("couldn't start capture\n");
3593 static int vino_streamoff(struct file *file, void *__fh,
3594 enum v4l2_buf_type i)
3596 struct vino_channel_settings *vcs = video_drvdata(file);
3600 if (!vcs->streaming)
3604 vino_capture_stop(vcs);
3609 static int vino_queryctrl(struct file *file, void *__fh,
3610 struct v4l2_queryctrl *queryctrl)
3612 struct vino_channel_settings *vcs = video_drvdata(file);
3613 unsigned long flags;
3617 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3619 switch (vcs->input) {
3621 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3622 if (vino_indycam_v4l2_controls[i].id ==
3625 &vino_indycam_v4l2_controls[i],
3626 sizeof(struct v4l2_queryctrl));
3627 queryctrl->reserved[0] = 0;
3634 case VINO_INPUT_COMPOSITE:
3635 case VINO_INPUT_SVIDEO:
3636 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3637 if (vino_saa7191_v4l2_controls[i].id ==
3640 &vino_saa7191_v4l2_controls[i],
3641 sizeof(struct v4l2_queryctrl));
3642 queryctrl->reserved[0] = 0;
3654 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3659 static int vino_g_ctrl(struct file *file, void *__fh,
3660 struct v4l2_control *control)
3662 struct vino_channel_settings *vcs = video_drvdata(file);
3663 unsigned long flags;
3667 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3669 switch (vcs->input) {
3670 case VINO_INPUT_D1: {
3672 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3673 if (vino_indycam_v4l2_controls[i].id == control->id) {
3682 err = i2c_camera_command(VIDIOC_G_CTRL, &control);
3687 case VINO_INPUT_COMPOSITE:
3688 case VINO_INPUT_SVIDEO: {
3690 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3691 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3700 err = i2c_decoder_command(VIDIOC_G_CTRL, &control);
3710 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3715 static int vino_s_ctrl(struct file *file, void *__fh,
3716 struct v4l2_control *control)
3718 struct vino_channel_settings *vcs = video_drvdata(file);
3719 unsigned long flags;
3723 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3725 if (!vino_is_input_owner(vcs)) {
3730 switch (vcs->input) {
3731 case VINO_INPUT_D1: {
3733 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3734 if (vino_indycam_v4l2_controls[i].id == control->id) {
3741 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3742 control->value > vino_indycam_v4l2_controls[i].maximum) {
3746 err = i2c_camera_command(VIDIOC_S_CTRL, &control);
3751 case VINO_INPUT_COMPOSITE:
3752 case VINO_INPUT_SVIDEO: {
3754 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3755 if (vino_saa7191_v4l2_controls[i].id == control->id) {
3762 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3763 control->value > vino_saa7191_v4l2_controls[i].maximum) {
3768 err = i2c_decoder_command(VIDIOC_S_CTRL, &control);
3778 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3783 /* File operations */
3785 static int vino_open(struct file *file)
3787 struct vino_channel_settings *vcs = video_drvdata(file);
3789 dprintk("open(): channel = %c\n",
3790 (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3792 mutex_lock(&vcs->mutex);
3795 dprintk("open(): driver busy\n");
3800 ret = vino_acquire_input(vcs);
3802 dprintk("open(): vino_acquire_input() failed\n");
3809 mutex_unlock(&vcs->mutex);
3811 dprintk("open(): %s!\n", ret ? "failed" : "complete");
3816 static int vino_close(struct file *file)
3818 struct vino_channel_settings *vcs = video_drvdata(file);
3819 dprintk("close():\n");
3821 mutex_lock(&vcs->mutex);
3826 vino_release_input(vcs);
3828 /* stop DMA and free buffers */
3829 vino_capture_stop(vcs);
3830 vino_queue_free(&vcs->fb_queue);
3833 mutex_unlock(&vcs->mutex);
3838 static void vino_vm_open(struct vm_area_struct *vma)
3840 struct vino_framebuffer *fb = vma->vm_private_data;
3843 dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3846 static void vino_vm_close(struct vm_area_struct *vma)
3848 struct vino_framebuffer *fb = vma->vm_private_data;
3851 dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3854 static struct vm_operations_struct vino_vm_ops = {
3855 .open = vino_vm_open,
3856 .close = vino_vm_close,
3859 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3861 struct vino_channel_settings *vcs = video_drvdata(file);
3863 unsigned long start = vma->vm_start;
3864 unsigned long size = vma->vm_end - vma->vm_start;
3865 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3867 struct vino_framebuffer *fb = NULL;
3868 unsigned int i, length;
3871 dprintk("mmap():\n");
3873 // TODO: reject mmap if already mapped
3875 if (mutex_lock_interruptible(&vcs->mutex))
3883 // TODO: check queue type
3885 if (!(vma->vm_flags & VM_WRITE)) {
3886 dprintk("mmap(): app bug: PROT_WRITE please\n");
3890 if (!(vma->vm_flags & VM_SHARED)) {
3891 dprintk("mmap(): app bug: MAP_SHARED please\n");
3896 /* find the correct buffer using offset */
3897 length = vino_queue_get_length(&vcs->fb_queue);
3899 dprintk("mmap(): queue not initialized\n");
3904 for (i = 0; i < length; i++) {
3905 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3907 dprintk("mmap(): vino_queue_get_buffer() failed\n");
3912 if (fb->offset == offset)
3916 dprintk("mmap(): invalid offset = %lu\n", offset);
3921 dprintk("mmap(): buffer = %d\n", i);
3923 if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3924 dprintk("mmap(): failed: size = %lu > %lu\n",
3925 size, fb->desc_table.page_count * PAGE_SIZE);
3930 for (i = 0; i < fb->desc_table.page_count; i++) {
3932 virt_to_phys((void *)fb->desc_table.virtual[i]) >>
3935 if (size < PAGE_SIZE)
3938 // protection was: PAGE_READONLY
3939 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
3940 vma->vm_page_prot)) {
3941 dprintk("mmap(): remap_pfn_range() failed\n");
3952 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
3953 vma->vm_flags &= ~VM_IO;
3954 vma->vm_private_data = fb;
3955 vma->vm_file = file;
3956 vma->vm_ops = &vino_vm_ops;
3959 mutex_unlock(&vcs->mutex);
3964 static unsigned int vino_poll(struct file *file, poll_table *pt)
3966 struct vino_channel_settings *vcs = video_drvdata(file);
3967 unsigned int outgoing;
3968 unsigned int ret = 0;
3971 // TODO: this has to be corrected for different read modes
3973 dprintk("poll():\n");
3975 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3976 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3983 poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
3985 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
3986 dprintk("poll(): vino_queue_get_outgoing() failed\n");
3992 dprintk("poll(): data %savailable\n",
3993 (outgoing > 0) ? "" : "not ");
3996 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 .unlocked_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 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
4106 kfree(vino_drvdata);
4112 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4117 static int vino_probe(void)
4119 unsigned long rev_id;
4121 if (ip22_is_fullhouse()) {
4122 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4126 if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4127 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4131 vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4133 printk(KERN_ERR "VINO: ioremap() failed\n");
4138 if (get_dbe(rev_id, &(vino->rev_id))) {
4139 printk(KERN_ERR "Failed to read VINO revision register\n");
4140 vino_module_cleanup(vino_init_stage);
4144 if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4145 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4147 vino_module_cleanup(vino_init_stage);
4151 printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
4156 static int vino_init(void)
4158 dma_addr_t dma_dummy_address;
4162 vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
4163 if (!vino_drvdata) {
4164 vino_module_cleanup(vino_init_stage);
4168 strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4169 sizeof(vino_drvdata->v4l2_dev.name));
4170 err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4175 /* create a dummy dma descriptor */
4176 vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4177 if (!vino_drvdata->dummy_page) {
4178 vino_module_cleanup(vino_init_stage);
4183 // TODO: use page_count in dummy_desc_table
4185 vino_drvdata->dummy_desc_table.dma_cpu =
4186 dma_alloc_coherent(NULL,
4187 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4188 &vino_drvdata->dummy_desc_table.dma,
4189 GFP_KERNEL | GFP_DMA);
4190 if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4191 vino_module_cleanup(vino_init_stage);
4196 dma_dummy_address = dma_map_single(NULL,
4197 (void *)vino_drvdata->dummy_page,
4198 PAGE_SIZE, DMA_FROM_DEVICE);
4199 for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4200 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4203 /* initialize VINO */
4206 vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4207 vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4208 udelay(VINO_DESC_FETCH_DELAY);
4210 vino->intr_status = 0;
4212 vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4213 vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4218 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4219 unsigned int channel, const char *name)
4221 vcs->channel = channel;
4222 vcs->input = VINO_INPUT_NONE;
4225 vcs->data_format = VINO_DATA_FMT_GREY;
4226 vcs->data_norm = VINO_DATA_NORM_NTSC;
4227 vcs->decimation = 1;
4228 vino_set_default_clipping(vcs);
4229 vino_set_default_framerate(vcs);
4233 mutex_init(&vcs->mutex);
4234 spin_lock_init(&vcs->capture_lock);
4236 mutex_init(&vcs->fb_queue.queue_mutex);
4237 spin_lock_init(&vcs->fb_queue.queue_lock);
4238 init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4240 vcs->vdev = video_device_alloc();
4242 vino_module_cleanup(vino_init_stage);
4247 memcpy(vcs->vdev, &vdev_template,
4248 sizeof(struct video_device));
4249 strcpy(vcs->vdev->name, name);
4250 vcs->vdev->release = video_device_release;
4251 vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
4253 video_set_drvdata(vcs->vdev, vcs);
4258 static int __init vino_module_init(void)
4262 printk(KERN_INFO "SGI VINO driver version %s\n",
4263 VINO_MODULE_VERSION);
4273 /* initialize data structures */
4275 spin_lock_init(&vino_drvdata->vino_lock);
4276 spin_lock_init(&vino_drvdata->input_lock);
4278 ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4283 ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4288 /* initialize hardware and register V4L devices */
4290 ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4291 vino_driver_description, NULL);
4293 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4295 vino_module_cleanup(vino_init_stage);
4300 ret = vino_i2c_add_bus();
4302 printk(KERN_ERR "VINO I2C bus registration failed\n");
4303 vino_module_cleanup(vino_init_stage);
4306 i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
4309 ret = video_register_device(vino_drvdata->a.vdev,
4310 VFL_TYPE_GRABBER, -1);
4312 printk(KERN_ERR "VINO channel A Video4Linux-device "
4313 "registration failed\n");
4314 vino_module_cleanup(vino_init_stage);
4319 ret = video_register_device(vino_drvdata->b.vdev,
4320 VFL_TYPE_GRABBER, -1);
4322 printk(KERN_ERR "VINO channel B Video4Linux-device "
4323 "registration failed\n");
4324 vino_module_cleanup(vino_init_stage);
4330 request_module("saa7191");
4331 request_module("indycam");
4334 dprintk("init complete!\n");
4339 static void __exit vino_module_exit(void)
4341 dprintk("exiting, stage = %d ...\n", vino_init_stage);
4342 vino_module_cleanup(vino_init_stage);
4343 dprintk("cleanup complete, exit!\n");
4346 module_init(vino_module_init);
4347 module_exit(vino_module_exit);