]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/vino.c
V4L/DVB (10868): vino: add note that this conversion is untested.
[karo-tx-linux.git] / drivers / media / video / vino.c
1 /*
2  * Driver for the VINO (Video In No Out) system found in SGI Indys.
3  *
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.
6  *
7  * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
8  *
9  * Based on the previous version of the driver for 2.4 kernels by:
10  * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
11  *
12  * v4l2_device/v4l2_subdev conversion by:
13  * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
14  *
15  * Note: this conversion is untested! Please contact the linux-media
16  * mailinglist if you can test this, together with the test results.
17  */
18
19 /*
20  * TODO:
21  * - remove "mark pages reserved-hacks" from memory allocation code
22  *   and implement fault()
23  * - check decimation, calculating and reporting image size when
24  *   using decimation
25  * - implement read(), user mode buffers and overlay (?)
26  */
27
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/errno.h>
33 #include <linux/fs.h>
34 #include <linux/interrupt.h>
35 #include <linux/kernel.h>
36 #include <linux/mm.h>
37 #include <linux/time.h>
38 #include <linux/version.h>
39 #include <linux/kmod.h>
40
41 #include <linux/i2c.h>
42
43 #include <linux/videodev2.h>
44 #include <media/v4l2-device.h>
45 #include <media/v4l2-ioctl.h>
46 #include <linux/mutex.h>
47
48 #include <asm/paccess.h>
49 #include <asm/io.h>
50 #include <asm/sgi/ip22.h>
51 #include <asm/sgi/mc.h>
52
53 #include "vino.h"
54 #include "saa7191.h"
55 #include "indycam.h"
56
57 /* Uncomment the following line to get lots and lots of (mostly useless)
58  * debug info.
59  * Note that the debug output also slows down the driver significantly */
60 // #define VINO_DEBUG
61 // #define VINO_DEBUG_INT
62
63 #define VINO_MODULE_VERSION "0.0.5"
64 #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
65
66 MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
67 MODULE_VERSION(VINO_MODULE_VERSION);
68 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
69 MODULE_LICENSE("GPL");
70
71 #ifdef VINO_DEBUG
72 #define dprintk(x...) printk("VINO: " x);
73 #else
74 #define dprintk(x...)
75 #endif
76
77 #define VINO_NO_CHANNEL                 0
78 #define VINO_CHANNEL_A                  1
79 #define VINO_CHANNEL_B                  2
80
81 #define VINO_PAL_WIDTH                  768
82 #define VINO_PAL_HEIGHT                 576
83 #define VINO_NTSC_WIDTH                 640
84 #define VINO_NTSC_HEIGHT                480
85
86 #define VINO_MIN_WIDTH                  32
87 #define VINO_MIN_HEIGHT                 32
88
89 #define VINO_CLIPPING_START_ODD_D1      1
90 #define VINO_CLIPPING_START_ODD_PAL     15
91 #define VINO_CLIPPING_START_ODD_NTSC    12
92
93 #define VINO_CLIPPING_START_EVEN_D1     2
94 #define VINO_CLIPPING_START_EVEN_PAL    15
95 #define VINO_CLIPPING_START_EVEN_NTSC   12
96
97 #define VINO_INPUT_CHANNEL_COUNT        3
98
99 /* the number is the index for vino_inputs */
100 #define VINO_INPUT_NONE                 -1
101 #define VINO_INPUT_COMPOSITE            0
102 #define VINO_INPUT_SVIDEO               1
103 #define VINO_INPUT_D1                   2
104
105 #define VINO_PAGE_RATIO                 (PAGE_SIZE / VINO_PAGE_SIZE)
106
107 #define VINO_FIFO_THRESHOLD_DEFAULT     16
108
109 #define VINO_FRAMEBUFFER_SIZE           ((VINO_PAL_WIDTH \
110                                           * VINO_PAL_HEIGHT * 4 \
111                                           + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
112
113 #define VINO_FRAMEBUFFER_COUNT_MAX      8
114
115 #define VINO_FRAMEBUFFER_UNUSED         0
116 #define VINO_FRAMEBUFFER_IN_USE         1
117 #define VINO_FRAMEBUFFER_READY          2
118
119 #define VINO_QUEUE_ERROR                -1
120 #define VINO_QUEUE_MAGIC                0x20050125
121
122 #define VINO_MEMORY_NONE                0
123 #define VINO_MEMORY_MMAP                1
124 #define VINO_MEMORY_USERPTR             2
125
126 #define VINO_DUMMY_DESC_COUNT           4
127 #define VINO_DESC_FETCH_DELAY           5       /* microseconds */
128
129 #define VINO_MAX_FRAME_SKIP_COUNT       128
130
131 /* the number is the index for vino_data_formats */
132 #define VINO_DATA_FMT_NONE              -1
133 #define VINO_DATA_FMT_GREY              0
134 #define VINO_DATA_FMT_RGB332            1
135 #define VINO_DATA_FMT_RGB32             2
136 #define VINO_DATA_FMT_YUV               3
137
138 #define VINO_DATA_FMT_COUNT             4
139
140 /* the number is the index for vino_data_norms */
141 #define VINO_DATA_NORM_NONE             -1
142 #define VINO_DATA_NORM_NTSC             0
143 #define VINO_DATA_NORM_PAL              1
144 #define VINO_DATA_NORM_SECAM            2
145 #define VINO_DATA_NORM_D1               3
146
147 #define VINO_DATA_NORM_COUNT            4
148
149 /* I2C controller flags */
150 #define SGI_I2C_FORCE_IDLE              (0 << 0)
151 #define SGI_I2C_NOT_IDLE                (1 << 0)
152 #define SGI_I2C_WRITE                   (0 << 1)
153 #define SGI_I2C_READ                    (1 << 1)
154 #define SGI_I2C_RELEASE_BUS             (0 << 2)
155 #define SGI_I2C_HOLD_BUS                (1 << 2)
156 #define SGI_I2C_XFER_DONE               (0 << 4)
157 #define SGI_I2C_XFER_BUSY               (1 << 4)
158 #define SGI_I2C_ACK                     (0 << 5)
159 #define SGI_I2C_NACK                    (1 << 5)
160 #define SGI_I2C_BUS_OK                  (0 << 7)
161 #define SGI_I2C_BUS_ERR                 (1 << 7)
162
163 /* Internal data structure definitions */
164
165 struct vino_input {
166         char *name;
167         v4l2_std_id std;
168 };
169
170 struct vino_clipping {
171         unsigned int left, right, top, bottom;
172 };
173
174 struct vino_data_format {
175         /* the description */
176         char *description;
177         /* bytes per pixel */
178         unsigned int bpp;
179         /* V4L2 fourcc code */
180         __u32 pixelformat;
181         /* V4L2 colorspace (duh!) */
182         enum v4l2_colorspace colorspace;
183 };
184
185 struct vino_data_norm {
186         char *description;
187         unsigned int width, height;
188         struct vino_clipping odd;
189         struct vino_clipping even;
190
191         v4l2_std_id std;
192         unsigned int fps_min, fps_max;
193         __u32 framelines;
194 };
195
196 struct vino_descriptor_table {
197         /* the number of PAGE_SIZE sized pages in the buffer */
198         unsigned int page_count;
199         /* virtual (kmalloc'd) pointers to the actual data
200          * (in PAGE_SIZE chunks, used with mmap streaming) */
201         unsigned long *virtual;
202
203         /* cpu address for the VINO descriptor table
204          * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
205         unsigned long *dma_cpu;
206         /* dma address for the VINO descriptor table
207          * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
208         dma_addr_t dma;
209 };
210
211 struct vino_framebuffer {
212         /* identifier nubmer */
213         unsigned int id;
214         /* the length of the whole buffer */
215         unsigned int size;
216         /* the length of actual data in buffer */
217         unsigned int data_size;
218         /* the data format */
219         unsigned int data_format;
220         /* the state of buffer data */
221         unsigned int state;
222         /* is the buffer mapped in user space? */
223         unsigned int map_count;
224         /* memory offset for mmap() */
225         unsigned int offset;
226         /* frame counter */
227         unsigned int frame_counter;
228         /* timestamp (written when image capture finishes) */
229         struct timeval timestamp;
230
231         struct vino_descriptor_table desc_table;
232
233         spinlock_t state_lock;
234 };
235
236 struct vino_framebuffer_fifo {
237         unsigned int length;
238
239         unsigned int used;
240         unsigned int head;
241         unsigned int tail;
242
243         unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
244 };
245
246 struct vino_framebuffer_queue {
247         unsigned int magic;
248
249         /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
250         unsigned int type;
251         unsigned int length;
252
253         /* data field of in and out contain index numbers for buffer */
254         struct vino_framebuffer_fifo in;
255         struct vino_framebuffer_fifo out;
256
257         struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
258
259         spinlock_t queue_lock;
260         struct mutex queue_mutex;
261         wait_queue_head_t frame_wait_queue;
262 };
263
264 struct vino_interrupt_data {
265         struct timeval timestamp;
266         unsigned int frame_counter;
267         unsigned int skip_count;
268         unsigned int skip;
269 };
270
271 struct vino_channel_settings {
272         unsigned int channel;
273
274         int input;
275         unsigned int data_format;
276         unsigned int data_norm;
277         struct vino_clipping clipping;
278         unsigned int decimation;
279         unsigned int line_size;
280         unsigned int alpha;
281         unsigned int fps;
282         unsigned int framert_reg;
283
284         unsigned int fifo_threshold;
285
286         struct vino_framebuffer_queue fb_queue;
287
288         /* number of the current field */
289         unsigned int field;
290
291         /* read in progress */
292         int reading;
293         /* streaming is active */
294         int streaming;
295         /* the driver is currently processing the queue */
296         int capturing;
297
298         struct mutex mutex;
299         spinlock_t capture_lock;
300
301         unsigned int users;
302
303         struct vino_interrupt_data int_data;
304
305         /* V4L support */
306         struct video_device *vdev;
307 };
308
309 struct vino_settings {
310         struct v4l2_device v4l2_dev;
311         struct vino_channel_settings a;
312         struct vino_channel_settings b;
313
314         /* the channel which owns this client:
315          * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
316         unsigned int decoder_owner;
317         struct v4l2_subdev *decoder;
318         unsigned int camera_owner;
319         struct v4l2_subdev *camera;
320
321         /* a lock for vino register access */
322         spinlock_t vino_lock;
323         /* a lock for channel input changes */
324         spinlock_t input_lock;
325
326         unsigned long dummy_page;
327         struct vino_descriptor_table dummy_desc_table;
328 };
329
330 /* Module parameters */
331
332 /*
333  * Using vino_pixel_conversion the ABGR32-format pixels supplied
334  * by the VINO chip can be converted to more common formats
335  * like RGBA32 (or probably RGB24 in the future). This way we
336  * can give out data that can be specified correctly with
337  * the V4L2-definitions.
338  *
339  * The pixel format is specified as RGBA32 when no conversion
340  * is used.
341  *
342  * Note that this only affects the 32-bit bit depth.
343  *
344  * Use non-zero value to enable conversion.
345  */
346 static int vino_pixel_conversion;
347
348 module_param_named(pixelconv, vino_pixel_conversion, int, 0);
349
350 MODULE_PARM_DESC(pixelconv,
351                  "enable pixel conversion (non-zero value enables)");
352
353 /* Internal data structures */
354
355 static struct sgi_vino *vino;
356
357 static struct vino_settings *vino_drvdata;
358
359 #define camera_call(o, f, args...) \
360         v4l2_subdev_call(vino_drvdata->camera, o, f, ##args)
361 #define decoder_call(o, f, args...) \
362         v4l2_subdev_call(vino_drvdata->decoder, o, f, ##args)
363
364 static const char *vino_driver_name = "vino";
365 static const char *vino_driver_description = "SGI VINO";
366 static const char *vino_bus_name = "GIO64 bus";
367 static const char *vino_vdev_name_a = "SGI VINO Channel A";
368 static const char *vino_vdev_name_b = "SGI VINO Channel B";
369
370 static void vino_capture_tasklet(unsigned long channel);
371
372 DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
373 DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
374
375 static const struct vino_input vino_inputs[] = {
376         {
377                 .name           = "Composite",
378                 .std            = V4L2_STD_NTSC | V4L2_STD_PAL
379                 | V4L2_STD_SECAM,
380         }, {
381                 .name           = "S-Video",
382                 .std            = V4L2_STD_NTSC | V4L2_STD_PAL
383                 | V4L2_STD_SECAM,
384         }, {
385                 .name           = "D1/IndyCam",
386                 .std            = V4L2_STD_NTSC,
387         }
388 };
389
390 static const struct vino_data_format vino_data_formats[] = {
391         {
392                 .description    = "8-bit greyscale",
393                 .bpp            = 1,
394                 .pixelformat    = V4L2_PIX_FMT_GREY,
395                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
396         }, {
397                 .description    = "8-bit dithered RGB 3-3-2",
398                 .bpp            = 1,
399                 .pixelformat    = V4L2_PIX_FMT_RGB332,
400                 .colorspace     = V4L2_COLORSPACE_SRGB,
401         }, {
402                 .description    = "32-bit RGB",
403                 .bpp            = 4,
404                 .pixelformat    = V4L2_PIX_FMT_RGB32,
405                 .colorspace     = V4L2_COLORSPACE_SRGB,
406         }, {
407                 .description    = "YUV 4:2:2",
408                 .bpp            = 2,
409                 .pixelformat    = V4L2_PIX_FMT_YUYV, // XXX: swapped?
410                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
411         }
412 };
413
414 static const struct vino_data_norm vino_data_norms[] = {
415         {
416                 .description    = "NTSC",
417                 .std            = V4L2_STD_NTSC,
418                 .fps_min        = 6,
419                 .fps_max        = 30,
420                 .framelines     = 525,
421                 .width          = VINO_NTSC_WIDTH,
422                 .height         = VINO_NTSC_HEIGHT,
423                 .odd            = {
424                         .top    = VINO_CLIPPING_START_ODD_NTSC,
425                         .left   = 0,
426                         .bottom = VINO_CLIPPING_START_ODD_NTSC
427                         + VINO_NTSC_HEIGHT / 2 - 1,
428                         .right  = VINO_NTSC_WIDTH,
429                 },
430                 .even           = {
431                         .top    = VINO_CLIPPING_START_EVEN_NTSC,
432                         .left   = 0,
433                         .bottom = VINO_CLIPPING_START_EVEN_NTSC
434                         + VINO_NTSC_HEIGHT / 2 - 1,
435                         .right  = VINO_NTSC_WIDTH,
436                 },
437         }, {
438                 .description    = "PAL",
439                 .std            = V4L2_STD_PAL,
440                 .fps_min        = 5,
441                 .fps_max        = 25,
442                 .framelines     = 625,
443                 .width          = VINO_PAL_WIDTH,
444                 .height         = VINO_PAL_HEIGHT,
445                 .odd            = {
446                         .top    = VINO_CLIPPING_START_ODD_PAL,
447                         .left   = 0,
448                         .bottom = VINO_CLIPPING_START_ODD_PAL
449                         + VINO_PAL_HEIGHT / 2 - 1,
450                         .right  = VINO_PAL_WIDTH,
451                 },
452                 .even           = {
453                         .top    = VINO_CLIPPING_START_EVEN_PAL,
454                         .left   = 0,
455                         .bottom = VINO_CLIPPING_START_EVEN_PAL
456                         + VINO_PAL_HEIGHT / 2 - 1,
457                         .right  = VINO_PAL_WIDTH,
458                 },
459         }, {
460                 .description    = "SECAM",
461                 .std            = V4L2_STD_SECAM,
462                 .fps_min        = 5,
463                 .fps_max        = 25,
464                 .framelines     = 625,
465                 .width          = VINO_PAL_WIDTH,
466                 .height         = VINO_PAL_HEIGHT,
467                 .odd            = {
468                         .top    = VINO_CLIPPING_START_ODD_PAL,
469                         .left   = 0,
470                         .bottom = VINO_CLIPPING_START_ODD_PAL
471                         + VINO_PAL_HEIGHT / 2 - 1,
472                         .right  = VINO_PAL_WIDTH,
473                 },
474                 .even           = {
475                         .top    = VINO_CLIPPING_START_EVEN_PAL,
476                         .left   = 0,
477                         .bottom = VINO_CLIPPING_START_EVEN_PAL
478                         + VINO_PAL_HEIGHT / 2 - 1,
479                         .right  = VINO_PAL_WIDTH,
480                 },
481         }, {
482                 .description    = "NTSC/D1",
483                 .std            = V4L2_STD_NTSC,
484                 .fps_min        = 6,
485                 .fps_max        = 30,
486                 .framelines     = 525,
487                 .width          = VINO_NTSC_WIDTH,
488                 .height         = VINO_NTSC_HEIGHT,
489                 .odd            = {
490                         .top    = VINO_CLIPPING_START_ODD_D1,
491                         .left   = 0,
492                         .bottom = VINO_CLIPPING_START_ODD_D1
493                         + VINO_NTSC_HEIGHT / 2 - 1,
494                         .right  = VINO_NTSC_WIDTH,
495                 },
496                 .even           = {
497                         .top    = VINO_CLIPPING_START_EVEN_D1,
498                         .left   = 0,
499                         .bottom = VINO_CLIPPING_START_EVEN_D1
500                         + VINO_NTSC_HEIGHT / 2 - 1,
501                         .right  = VINO_NTSC_WIDTH,
502                 },
503         }
504 };
505
506 #define VINO_INDYCAM_V4L2_CONTROL_COUNT         9
507
508 struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
509         {
510                 .id = V4L2_CID_AUTOGAIN,
511                 .type = V4L2_CTRL_TYPE_BOOLEAN,
512                 .name = "Automatic Gain Control",
513                 .minimum = 0,
514                 .maximum = 1,
515                 .step = 1,
516                 .default_value = INDYCAM_AGC_DEFAULT,
517         }, {
518                 .id = V4L2_CID_AUTO_WHITE_BALANCE,
519                 .type = V4L2_CTRL_TYPE_BOOLEAN,
520                 .name = "Automatic White Balance",
521                 .minimum = 0,
522                 .maximum = 1,
523                 .step = 1,
524                 .default_value = INDYCAM_AWB_DEFAULT,
525         }, {
526                 .id = V4L2_CID_GAIN,
527                 .type = V4L2_CTRL_TYPE_INTEGER,
528                 .name = "Gain",
529                 .minimum = INDYCAM_GAIN_MIN,
530                 .maximum = INDYCAM_GAIN_MAX,
531                 .step = 1,
532                 .default_value = INDYCAM_GAIN_DEFAULT,
533         }, {
534                 .id = INDYCAM_CONTROL_RED_SATURATION,
535                 .type = V4L2_CTRL_TYPE_INTEGER,
536                 .name = "Red Saturation",
537                 .minimum = INDYCAM_RED_SATURATION_MIN,
538                 .maximum = INDYCAM_RED_SATURATION_MAX,
539                 .step = 1,
540                 .default_value = INDYCAM_RED_SATURATION_DEFAULT,
541         }, {
542                 .id = INDYCAM_CONTROL_BLUE_SATURATION,
543                 .type = V4L2_CTRL_TYPE_INTEGER,
544                 .name = "Blue Saturation",
545                 .minimum = INDYCAM_BLUE_SATURATION_MIN,
546                 .maximum = INDYCAM_BLUE_SATURATION_MAX,
547                 .step = 1,
548                 .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
549         }, {
550                 .id = V4L2_CID_RED_BALANCE,
551                 .type = V4L2_CTRL_TYPE_INTEGER,
552                 .name = "Red Balance",
553                 .minimum = INDYCAM_RED_BALANCE_MIN,
554                 .maximum = INDYCAM_RED_BALANCE_MAX,
555                 .step = 1,
556                 .default_value = INDYCAM_RED_BALANCE_DEFAULT,
557         }, {
558                 .id = V4L2_CID_BLUE_BALANCE,
559                 .type = V4L2_CTRL_TYPE_INTEGER,
560                 .name = "Blue Balance",
561                 .minimum = INDYCAM_BLUE_BALANCE_MIN,
562                 .maximum = INDYCAM_BLUE_BALANCE_MAX,
563                 .step = 1,
564                 .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
565         }, {
566                 .id = V4L2_CID_EXPOSURE,
567                 .type = V4L2_CTRL_TYPE_INTEGER,
568                 .name = "Shutter Control",
569                 .minimum = INDYCAM_SHUTTER_MIN,
570                 .maximum = INDYCAM_SHUTTER_MAX,
571                 .step = 1,
572                 .default_value = INDYCAM_SHUTTER_DEFAULT,
573         }, {
574                 .id = V4L2_CID_GAMMA,
575                 .type = V4L2_CTRL_TYPE_INTEGER,
576                 .name = "Gamma",
577                 .minimum = INDYCAM_GAMMA_MIN,
578                 .maximum = INDYCAM_GAMMA_MAX,
579                 .step = 1,
580                 .default_value = INDYCAM_GAMMA_DEFAULT,
581         }
582 };
583
584 #define VINO_SAA7191_V4L2_CONTROL_COUNT         9
585
586 struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
587         {
588                 .id = V4L2_CID_HUE,
589                 .type = V4L2_CTRL_TYPE_INTEGER,
590                 .name = "Hue",
591                 .minimum = SAA7191_HUE_MIN,
592                 .maximum = SAA7191_HUE_MAX,
593                 .step = 1,
594                 .default_value = SAA7191_HUE_DEFAULT,
595         }, {
596                 .id = SAA7191_CONTROL_BANDPASS,
597                 .type = V4L2_CTRL_TYPE_INTEGER,
598                 .name = "Luminance Bandpass",
599                 .minimum = SAA7191_BANDPASS_MIN,
600                 .maximum = SAA7191_BANDPASS_MAX,
601                 .step = 1,
602                 .default_value = SAA7191_BANDPASS_DEFAULT,
603         }, {
604                 .id = SAA7191_CONTROL_BANDPASS_WEIGHT,
605                 .type = V4L2_CTRL_TYPE_INTEGER,
606                 .name = "Luminance Bandpass Weight",
607                 .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
608                 .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
609                 .step = 1,
610                 .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
611         }, {
612                 .id = SAA7191_CONTROL_CORING,
613                 .type = V4L2_CTRL_TYPE_INTEGER,
614                 .name = "HF Luminance Coring",
615                 .minimum = SAA7191_CORING_MIN,
616                 .maximum = SAA7191_CORING_MAX,
617                 .step = 1,
618                 .default_value = SAA7191_CORING_DEFAULT,
619         }, {
620                 .id = SAA7191_CONTROL_FORCE_COLOUR,
621                 .type = V4L2_CTRL_TYPE_BOOLEAN,
622                 .name = "Force Colour",
623                 .minimum = SAA7191_FORCE_COLOUR_MIN,
624                 .maximum = SAA7191_FORCE_COLOUR_MAX,
625                 .step = 1,
626                 .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
627         }, {
628                 .id = SAA7191_CONTROL_CHROMA_GAIN,
629                 .type = V4L2_CTRL_TYPE_INTEGER,
630                 .name = "Chrominance Gain Control",
631                 .minimum = SAA7191_CHROMA_GAIN_MIN,
632                 .maximum = SAA7191_CHROMA_GAIN_MAX,
633                 .step = 1,
634                 .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
635         }, {
636                 .id = SAA7191_CONTROL_VTRC,
637                 .type = V4L2_CTRL_TYPE_BOOLEAN,
638                 .name = "VTR Time Constant",
639                 .minimum = SAA7191_VTRC_MIN,
640                 .maximum = SAA7191_VTRC_MAX,
641                 .step = 1,
642                 .default_value = SAA7191_VTRC_DEFAULT,
643         }, {
644                 .id = SAA7191_CONTROL_LUMA_DELAY,
645                 .type = V4L2_CTRL_TYPE_INTEGER,
646                 .name = "Luminance Delay Compensation",
647                 .minimum = SAA7191_LUMA_DELAY_MIN,
648                 .maximum = SAA7191_LUMA_DELAY_MAX,
649                 .step = 1,
650                 .default_value = SAA7191_LUMA_DELAY_DEFAULT,
651         }, {
652                 .id = SAA7191_CONTROL_VNR,
653                 .type = V4L2_CTRL_TYPE_INTEGER,
654                 .name = "Vertical Noise Reduction",
655                 .minimum = SAA7191_VNR_MIN,
656                 .maximum = SAA7191_VNR_MAX,
657                 .step = 1,
658                 .default_value = SAA7191_VNR_DEFAULT,
659         }
660 };
661
662 /* VINO framebuffer/DMA descriptor management */
663
664 static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
665                                                unsigned int count)
666 {
667         unsigned int i;
668
669         dprintk("vino_free_buffer_with_count(): count = %d\n", count);
670
671         for (i = 0; i < count; i++) {
672                 ClearPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
673                 dma_unmap_single(NULL,
674                                  fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
675                                  PAGE_SIZE, DMA_FROM_DEVICE);
676                 free_page(fb->desc_table.virtual[i]);
677         }
678
679         dma_free_coherent(NULL,
680                           VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
681                           sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
682                           fb->desc_table.dma);
683         kfree(fb->desc_table.virtual);
684
685         memset(fb, 0, sizeof(struct vino_framebuffer));
686 }
687
688 static void vino_free_buffer(struct vino_framebuffer *fb)
689 {
690         vino_free_buffer_with_count(fb, fb->desc_table.page_count);
691 }
692
693 static int vino_allocate_buffer(struct vino_framebuffer *fb,
694                                 unsigned int size)
695 {
696         unsigned int count, i, j;
697         int ret = 0;
698
699         dprintk("vino_allocate_buffer():\n");
700
701         if (size < 1)
702                 return -EINVAL;
703
704         memset(fb, 0, sizeof(struct vino_framebuffer));
705
706         count = ((size / PAGE_SIZE) + 4) & ~3;
707
708         dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
709                 size, count);
710
711         /* allocate memory for table with virtual (page) addresses */
712         fb->desc_table.virtual = (unsigned long *)
713                 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
714         if (!fb->desc_table.virtual)
715                 return -ENOMEM;
716
717         /* allocate memory for table with dma addresses
718          * (has space for four extra descriptors) */
719         fb->desc_table.dma_cpu =
720                 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
721                                    sizeof(dma_addr_t), &fb->desc_table.dma,
722                                    GFP_KERNEL | GFP_DMA);
723         if (!fb->desc_table.dma_cpu) {
724                 ret = -ENOMEM;
725                 goto out_free_virtual;
726         }
727
728         /* allocate pages for the buffer and acquire the according
729          * dma addresses */
730         for (i = 0; i < count; i++) {
731                 dma_addr_t dma_data_addr;
732
733                 fb->desc_table.virtual[i] =
734                         get_zeroed_page(GFP_KERNEL | GFP_DMA);
735                 if (!fb->desc_table.virtual[i]) {
736                         ret = -ENOBUFS;
737                         break;
738                 }
739
740                 dma_data_addr =
741                         dma_map_single(NULL,
742                                        (void *)fb->desc_table.virtual[i],
743                                        PAGE_SIZE, DMA_FROM_DEVICE);
744
745                 for (j = 0; j < VINO_PAGE_RATIO; j++) {
746                         fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
747                                 dma_data_addr + VINO_PAGE_SIZE * j;
748                 }
749
750                 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
751         }
752
753         /* page_count needs to be set anyway, because the descriptor table has
754          * been allocated according to this number */
755         fb->desc_table.page_count = count;
756
757         if (ret) {
758                 /* the descriptor with index i doesn't contain
759                  * a valid address yet */
760                 vino_free_buffer_with_count(fb, i);
761                 return ret;
762         }
763
764         //fb->size = size;
765         fb->size = count * PAGE_SIZE;
766         fb->data_format = VINO_DATA_FMT_NONE;
767
768         /* set the dma stop-bit for the last (count+1)th descriptor */
769         fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
770         return 0;
771
772  out_free_virtual:
773         kfree(fb->desc_table.virtual);
774         return ret;
775 }
776
777 #if 0
778 /* user buffers not fully implemented yet */
779 static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
780                                      void *user,
781                                      unsigned int size)
782 {
783         unsigned int count, i, j;
784         int ret = 0;
785
786         dprintk("vino_prepare_user_buffer():\n");
787
788         if (size < 1)
789                 return -EINVAL;
790
791         memset(fb, 0, sizeof(struct vino_framebuffer));
792
793         count = ((size / PAGE_SIZE)) & ~3;
794
795         dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
796                 size, count);
797
798         /* allocate memory for table with virtual (page) addresses */
799         fb->desc_table.virtual = (unsigned long *)
800                 kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
801         if (!fb->desc_table.virtual)
802                 return -ENOMEM;
803
804         /* allocate memory for table with dma addresses
805          * (has space for four extra descriptors) */
806         fb->desc_table.dma_cpu =
807                 dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
808                                    sizeof(dma_addr_t), &fb->desc_table.dma,
809                                    GFP_KERNEL | GFP_DMA);
810         if (!fb->desc_table.dma_cpu) {
811                 ret = -ENOMEM;
812                 goto out_free_virtual;
813         }
814
815         /* allocate pages for the buffer and acquire the according
816          * dma addresses */
817         for (i = 0; i < count; i++) {
818                 dma_addr_t dma_data_addr;
819
820                 fb->desc_table.virtual[i] =
821                         get_zeroed_page(GFP_KERNEL | GFP_DMA);
822                 if (!fb->desc_table.virtual[i]) {
823                         ret = -ENOBUFS;
824                         break;
825                 }
826
827                 dma_data_addr =
828                         dma_map_single(NULL,
829                                        (void *)fb->desc_table.virtual[i],
830                                        PAGE_SIZE, DMA_FROM_DEVICE);
831
832                 for (j = 0; j < VINO_PAGE_RATIO; j++) {
833                         fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
834                                 dma_data_addr + VINO_PAGE_SIZE * j;
835                 }
836
837                 SetPageReserved(virt_to_page((void *)fb->desc_table.virtual[i]));
838         }
839
840         /* page_count needs to be set anyway, because the descriptor table has
841          * been allocated according to this number */
842         fb->desc_table.page_count = count;
843
844         if (ret) {
845                 /* the descriptor with index i doesn't contain
846                  * a valid address yet */
847                 vino_free_buffer_with_count(fb, i);
848                 return ret;
849         }
850
851         //fb->size = size;
852         fb->size = count * PAGE_SIZE;
853
854         /* set the dma stop-bit for the last (count+1)th descriptor */
855         fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
856         return 0;
857
858  out_free_virtual:
859         kfree(fb->desc_table.virtual);
860         return ret;
861 }
862 #endif
863
864 static void vino_sync_buffer(struct vino_framebuffer *fb)
865 {
866         int i;
867
868         dprintk("vino_sync_buffer():\n");
869
870         for (i = 0; i < fb->desc_table.page_count; i++)
871                 dma_sync_single(NULL,
872                                 fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
873                                 PAGE_SIZE, DMA_FROM_DEVICE);
874 }
875
876 /* Framebuffer fifo functions (need to be locked externally) */
877
878 static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
879                            unsigned int length)
880 {
881         f->length = 0;
882         f->used = 0;
883         f->head = 0;
884         f->tail = 0;
885
886         if (length > VINO_FRAMEBUFFER_COUNT_MAX)
887                 length = VINO_FRAMEBUFFER_COUNT_MAX;
888
889         f->length = length;
890 }
891
892 /* returns true/false */
893 static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
894                                    unsigned int id)
895 {
896         unsigned int i;
897
898         for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
899                 if (f->data[i] == id)
900                         return 1;
901         }
902
903         return 0;
904 }
905
906 #if 0
907 /* returns true/false */
908 static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
909 {
910         return (f->used == f->length);
911 }
912 #endif
913
914 static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
915 {
916         return f->used;
917 }
918
919 static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
920 {
921         if (id >= f->length) {
922                 return VINO_QUEUE_ERROR;
923         }
924
925         if (vino_fifo_has_id(f, id)) {
926                 return VINO_QUEUE_ERROR;
927         }
928
929         if (f->used < f->length) {
930                 f->data[f->tail] = id;
931                 f->tail = (f->tail + 1) % f->length;
932                 f->used++;
933         } else {
934                 return VINO_QUEUE_ERROR;
935         }
936
937         return 0;
938 }
939
940 static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
941 {
942         if (f->used > 0) {
943                 *id = f->data[f->head];
944         } else {
945                 return VINO_QUEUE_ERROR;
946         }
947
948         return 0;
949 }
950
951 static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
952 {
953         if (f->used > 0) {
954                 *id = f->data[f->head];
955                 f->head = (f->head + 1) % f->length;
956                 f->used--;
957         } else {
958                 return VINO_QUEUE_ERROR;
959         }
960
961         return 0;
962 }
963
964 /* Framebuffer queue functions */
965
966 /* execute with queue_lock locked */
967 static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
968                                        unsigned int length)
969 {
970         unsigned int i;
971
972         q->length = 0;
973         memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
974         memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
975         for (i = 0; i < length; i++) {
976                 dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
977                         i);
978                 vino_free_buffer(q->buffer[i]);
979                 kfree(q->buffer[i]);
980         }
981
982         q->type = VINO_MEMORY_NONE;
983         q->magic = 0;
984 }
985
986 static void vino_queue_free(struct vino_framebuffer_queue *q)
987 {
988         dprintk("vino_queue_free():\n");
989
990         if (q->magic != VINO_QUEUE_MAGIC)
991                 return;
992         if (q->type != VINO_MEMORY_MMAP)
993                 return;
994
995         mutex_lock(&q->queue_mutex);
996
997         vino_queue_free_with_count(q, q->length);
998
999         mutex_unlock(&q->queue_mutex);
1000 }
1001
1002 static int vino_queue_init(struct vino_framebuffer_queue *q,
1003                            unsigned int *length)
1004 {
1005         unsigned int i;
1006         int ret = 0;
1007
1008         dprintk("vino_queue_init(): length = %d\n", *length);
1009
1010         if (q->magic == VINO_QUEUE_MAGIC) {
1011                 dprintk("vino_queue_init(): queue already initialized!\n");
1012                 return -EINVAL;
1013         }
1014
1015         if (q->type != VINO_MEMORY_NONE) {
1016                 dprintk("vino_queue_init(): queue already initialized!\n");
1017                 return -EINVAL;
1018         }
1019
1020         if (*length < 1)
1021                 return -EINVAL;
1022
1023         mutex_lock(&q->queue_mutex);
1024
1025         if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
1026                 *length = VINO_FRAMEBUFFER_COUNT_MAX;
1027
1028         q->length = 0;
1029
1030         for (i = 0; i < *length; i++) {
1031                 dprintk("vino_queue_init(): allocating buffer %d\n", i);
1032                 q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
1033                                        GFP_KERNEL);
1034                 if (!q->buffer[i]) {
1035                         dprintk("vino_queue_init(): kmalloc() failed\n");
1036                         ret = -ENOMEM;
1037                         break;
1038                 }
1039
1040                 ret = vino_allocate_buffer(q->buffer[i],
1041                                            VINO_FRAMEBUFFER_SIZE);
1042                 if (ret) {
1043                         kfree(q->buffer[i]);
1044                         dprintk("vino_queue_init(): "
1045                                 "vino_allocate_buffer() failed\n");
1046                         break;
1047                 }
1048
1049                 q->buffer[i]->id = i;
1050                 if (i > 0) {
1051                         q->buffer[i]->offset = q->buffer[i - 1]->offset +
1052                                 q->buffer[i - 1]->size;
1053                 } else {
1054                         q->buffer[i]->offset = 0;
1055                 }
1056
1057                 spin_lock_init(&q->buffer[i]->state_lock);
1058
1059                 dprintk("vino_queue_init(): buffer = %d, offset = %d, "
1060                         "size = %d\n", i, q->buffer[i]->offset,
1061                         q->buffer[i]->size);
1062         }
1063
1064         if (ret) {
1065                 vino_queue_free_with_count(q, i);
1066                 *length = 0;
1067         } else {
1068                 q->length = *length;
1069                 vino_fifo_init(&q->in, q->length);
1070                 vino_fifo_init(&q->out, q->length);
1071                 q->type = VINO_MEMORY_MMAP;
1072                 q->magic = VINO_QUEUE_MAGIC;
1073         }
1074
1075         mutex_unlock(&q->queue_mutex);
1076
1077         return ret;
1078 }
1079
1080 static struct vino_framebuffer *vino_queue_add(struct
1081                                                vino_framebuffer_queue *q,
1082                                                unsigned int id)
1083 {
1084         struct vino_framebuffer *ret = NULL;
1085         unsigned int total;
1086         unsigned long flags;
1087
1088         dprintk("vino_queue_add(): id = %d\n", id);
1089
1090         if (q->magic != VINO_QUEUE_MAGIC) {
1091                 return ret;
1092         }
1093
1094         spin_lock_irqsave(&q->queue_lock, flags);
1095
1096         if (q->length == 0)
1097                 goto out;
1098
1099         if (id >= q->length)
1100                 goto out;
1101
1102         /* not needed?: if (vino_fifo_full(&q->out)) {
1103                 goto out;
1104                 }*/
1105         /* check that outgoing queue isn't already full
1106          * (or that it won't become full) */
1107         total = vino_fifo_get_used(&q->in) +
1108                 vino_fifo_get_used(&q->out);
1109         if (total >= q->length)
1110                 goto out;
1111
1112         if (vino_fifo_enqueue(&q->in, id))
1113                 goto out;
1114
1115         ret = q->buffer[id];
1116
1117 out:
1118         spin_unlock_irqrestore(&q->queue_lock, flags);
1119
1120         return ret;
1121 }
1122
1123 static struct vino_framebuffer *vino_queue_transfer(struct
1124                                                     vino_framebuffer_queue *q)
1125 {
1126         struct vino_framebuffer *ret = NULL;
1127         struct vino_framebuffer *fb;
1128         int id;
1129         unsigned long flags;
1130
1131         dprintk("vino_queue_transfer():\n");
1132
1133         if (q->magic != VINO_QUEUE_MAGIC) {
1134                 return ret;
1135         }
1136
1137         spin_lock_irqsave(&q->queue_lock, flags);
1138
1139         if (q->length == 0)
1140                 goto out;
1141
1142         // now this actually removes an entry from the incoming queue
1143         if (vino_fifo_dequeue(&q->in, &id)) {
1144                 goto out;
1145         }
1146
1147         dprintk("vino_queue_transfer(): id = %d\n", id);
1148         fb = q->buffer[id];
1149
1150         // we have already checked that the outgoing queue is not full, but...
1151         if (vino_fifo_enqueue(&q->out, id)) {
1152                 printk(KERN_ERR "vino_queue_transfer(): "
1153                        "outgoing queue is full, this shouldn't happen!\n");
1154                 goto out;
1155         }
1156
1157         ret = fb;
1158 out:
1159         spin_unlock_irqrestore(&q->queue_lock, flags);
1160
1161         return ret;
1162 }
1163
1164 /* returns true/false */
1165 static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
1166                                         unsigned int id)
1167 {
1168         int ret = 0;
1169         unsigned long flags;
1170
1171         if (q->magic != VINO_QUEUE_MAGIC) {
1172                 return ret;
1173         }
1174
1175         spin_lock_irqsave(&q->queue_lock, flags);
1176
1177         if (q->length == 0)
1178                 goto out;
1179
1180         ret = vino_fifo_has_id(&q->in, id);
1181
1182 out:
1183         spin_unlock_irqrestore(&q->queue_lock, flags);
1184
1185         return ret;
1186 }
1187
1188 /* returns true/false */
1189 static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
1190                                         unsigned int id)
1191 {
1192         int ret = 0;
1193         unsigned long flags;
1194
1195         if (q->magic != VINO_QUEUE_MAGIC) {
1196                 return ret;
1197         }
1198
1199         spin_lock_irqsave(&q->queue_lock, flags);
1200
1201         if (q->length == 0)
1202                 goto out;
1203
1204         ret = vino_fifo_has_id(&q->out, id);
1205
1206 out:
1207         spin_unlock_irqrestore(&q->queue_lock, flags);
1208
1209         return ret;
1210 }
1211
1212 static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
1213                                    unsigned int *used)
1214 {
1215         int ret = 0;
1216         unsigned long flags;
1217
1218         if (q->magic != VINO_QUEUE_MAGIC) {
1219                 return VINO_QUEUE_ERROR;
1220         }
1221
1222         spin_lock_irqsave(&q->queue_lock, flags);
1223
1224         if (q->length == 0) {
1225                 ret = VINO_QUEUE_ERROR;
1226                 goto out;
1227         }
1228
1229         *used = vino_fifo_get_used(&q->in);
1230
1231 out:
1232         spin_unlock_irqrestore(&q->queue_lock, flags);
1233
1234         return ret;
1235 }
1236
1237 static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
1238                                    unsigned int *used)
1239 {
1240         int ret = 0;
1241         unsigned long flags;
1242
1243         if (q->magic != VINO_QUEUE_MAGIC) {
1244                 return VINO_QUEUE_ERROR;
1245         }
1246
1247         spin_lock_irqsave(&q->queue_lock, flags);
1248
1249         if (q->length == 0) {
1250                 ret = VINO_QUEUE_ERROR;
1251                 goto out;
1252         }
1253
1254         *used = vino_fifo_get_used(&q->out);
1255
1256 out:
1257         spin_unlock_irqrestore(&q->queue_lock, flags);
1258
1259         return ret;
1260 }
1261
1262 #if 0
1263 static int vino_queue_get_total(struct vino_framebuffer_queue *q,
1264                                 unsigned int *total)
1265 {
1266         int ret = 0;
1267         unsigned long flags;
1268
1269         if (q->magic != VINO_QUEUE_MAGIC) {
1270                 return VINO_QUEUE_ERROR;
1271         }
1272
1273         spin_lock_irqsave(&q->queue_lock, flags);
1274
1275         if (q->length == 0) {
1276                 ret = VINO_QUEUE_ERROR;
1277                 goto out;
1278         }
1279
1280         *total = vino_fifo_get_used(&q->in) +
1281                 vino_fifo_get_used(&q->out);
1282
1283 out:
1284         spin_unlock_irqrestore(&q->queue_lock, flags);
1285
1286         return ret;
1287 }
1288 #endif
1289
1290 static struct vino_framebuffer *vino_queue_peek(struct
1291                                                 vino_framebuffer_queue *q,
1292                                                 unsigned int *id)
1293 {
1294         struct vino_framebuffer *ret = NULL;
1295         unsigned long flags;
1296
1297         if (q->magic != VINO_QUEUE_MAGIC) {
1298                 return ret;
1299         }
1300
1301         spin_lock_irqsave(&q->queue_lock, flags);
1302
1303         if (q->length == 0)
1304                 goto out;
1305
1306         if (vino_fifo_peek(&q->in, id)) {
1307                 goto out;
1308         }
1309
1310         ret = q->buffer[*id];
1311 out:
1312         spin_unlock_irqrestore(&q->queue_lock, flags);
1313
1314         return ret;
1315 }
1316
1317 static struct vino_framebuffer *vino_queue_remove(struct
1318                                                   vino_framebuffer_queue *q,
1319                                                   unsigned int *id)
1320 {
1321         struct vino_framebuffer *ret = NULL;
1322         unsigned long flags;
1323         dprintk("vino_queue_remove():\n");
1324
1325         if (q->magic != VINO_QUEUE_MAGIC) {
1326                 return ret;
1327         }
1328
1329         spin_lock_irqsave(&q->queue_lock, flags);
1330
1331         if (q->length == 0)
1332                 goto out;
1333
1334         if (vino_fifo_dequeue(&q->out, id)) {
1335                 goto out;
1336         }
1337
1338         dprintk("vino_queue_remove(): id = %d\n", *id);
1339         ret = q->buffer[*id];
1340 out:
1341         spin_unlock_irqrestore(&q->queue_lock, flags);
1342
1343         return ret;
1344 }
1345
1346 static struct
1347 vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
1348                                         unsigned int id)
1349 {
1350         struct vino_framebuffer *ret = NULL;
1351         unsigned long flags;
1352
1353         if (q->magic != VINO_QUEUE_MAGIC) {
1354                 return ret;
1355         }
1356
1357         spin_lock_irqsave(&q->queue_lock, flags);
1358
1359         if (q->length == 0)
1360                 goto out;
1361
1362         if (id >= q->length)
1363                 goto out;
1364
1365         ret = q->buffer[id];
1366  out:
1367         spin_unlock_irqrestore(&q->queue_lock, flags);
1368
1369         return ret;
1370 }
1371
1372 static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
1373 {
1374         unsigned int length = 0;
1375         unsigned long flags;
1376
1377         if (q->magic != VINO_QUEUE_MAGIC) {
1378                 return length;
1379         }
1380
1381         spin_lock_irqsave(&q->queue_lock, flags);
1382         length = q->length;
1383         spin_unlock_irqrestore(&q->queue_lock, flags);
1384
1385         return length;
1386 }
1387
1388 static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
1389 {
1390         unsigned int i;
1391         int ret = 0;
1392         unsigned long flags;
1393
1394         if (q->magic != VINO_QUEUE_MAGIC) {
1395                 return ret;
1396         }
1397
1398         spin_lock_irqsave(&q->queue_lock, flags);
1399         for (i = 0; i < q->length; i++) {
1400                 if (q->buffer[i]->map_count > 0) {
1401                         ret = 1;
1402                         break;
1403                 }
1404         }
1405         spin_unlock_irqrestore(&q->queue_lock, flags);
1406
1407         return ret;
1408 }
1409
1410 /* VINO functions */
1411
1412 /* execute with input_lock locked */
1413 static void vino_update_line_size(struct vino_channel_settings *vcs)
1414 {
1415         unsigned int w = vcs->clipping.right - vcs->clipping.left;
1416         unsigned int d = vcs->decimation;
1417         unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
1418         unsigned int lsize;
1419
1420         dprintk("update_line_size(): before: w = %d, d = %d, "
1421                 "line_size = %d\n", w, d, vcs->line_size);
1422
1423         /* line size must be multiple of 8 bytes */
1424         lsize = (bpp * (w / d)) & ~7;
1425         w = (lsize / bpp) * d;
1426
1427         vcs->clipping.right = vcs->clipping.left + w;
1428         vcs->line_size = lsize;
1429
1430         dprintk("update_line_size(): after: w = %d, d = %d, "
1431                 "line_size = %d\n", w, d, vcs->line_size);
1432 }
1433
1434 /* execute with input_lock locked */
1435 static void vino_set_clipping(struct vino_channel_settings *vcs,
1436                               unsigned int x, unsigned int y,
1437                               unsigned int w, unsigned int h)
1438 {
1439         unsigned int maxwidth, maxheight;
1440         unsigned int d;
1441
1442         maxwidth = vino_data_norms[vcs->data_norm].width;
1443         maxheight = vino_data_norms[vcs->data_norm].height;
1444         d = vcs->decimation;
1445
1446         y &= ~1;        /* odd/even fields */
1447
1448         if (x > maxwidth) {
1449                 x = 0;
1450         }
1451         if (y > maxheight) {
1452                 y = 0;
1453         }
1454
1455         if (((w / d) < VINO_MIN_WIDTH)
1456             || ((h / d) < VINO_MIN_HEIGHT)) {
1457                 w = VINO_MIN_WIDTH * d;
1458                 h = VINO_MIN_HEIGHT * d;
1459         }
1460
1461         if ((x + w) > maxwidth) {
1462                 w = maxwidth - x;
1463                 if ((w / d) < VINO_MIN_WIDTH)
1464                         x = maxwidth - VINO_MIN_WIDTH * d;
1465         }
1466         if ((y + h) > maxheight) {
1467                 h = maxheight - y;
1468                 if ((h / d) < VINO_MIN_HEIGHT)
1469                         y = maxheight - VINO_MIN_HEIGHT * d;
1470         }
1471
1472         vcs->clipping.left = x;
1473         vcs->clipping.top = y;
1474         vcs->clipping.right = x + w;
1475         vcs->clipping.bottom = y + h;
1476
1477         vino_update_line_size(vcs);
1478
1479         dprintk("clipping %d, %d, %d, %d / %d - %d\n",
1480                 vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
1481                 vcs->clipping.bottom, vcs->decimation, vcs->line_size);
1482 }
1483
1484 /* execute with input_lock locked */
1485 static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
1486 {
1487         vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
1488                           vino_data_norms[vcs->data_norm].height);
1489 }
1490
1491 /* execute with input_lock locked */
1492 static void vino_set_scaling(struct vino_channel_settings *vcs,
1493                              unsigned int w, unsigned int h)
1494 {
1495         unsigned int x, y, curw, curh, d;
1496
1497         x = vcs->clipping.left;
1498         y = vcs->clipping.top;
1499         curw = vcs->clipping.right - vcs->clipping.left;
1500         curh = vcs->clipping.bottom - vcs->clipping.top;
1501
1502         d = max(curw / w, curh / h);
1503
1504         dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
1505                 w, h, curw, curh, d);
1506
1507         if (d < 1) {
1508                 d = 1;
1509         } else if (d > 8) {
1510                 d = 8;
1511         }
1512
1513         vcs->decimation = d;
1514         vino_set_clipping(vcs, x, y, w * d, h * d);
1515
1516         dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
1517                 vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
1518                 vcs->decimation, vcs->line_size);
1519 }
1520
1521 /* execute with input_lock locked */
1522 static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
1523 {
1524         vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
1525                          vcs->clipping.bottom - vcs->clipping.top);
1526 }
1527
1528 /* execute with input_lock locked */
1529 static void vino_set_framerate(struct vino_channel_settings *vcs,
1530                                unsigned int fps)
1531 {
1532         unsigned int mask;
1533
1534         switch (vcs->data_norm) {
1535         case VINO_DATA_NORM_NTSC:
1536         case VINO_DATA_NORM_D1:
1537                 fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
1538
1539                 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1540                         fps = vino_data_norms[vcs->data_norm].fps_min;
1541                 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1542                         fps = vino_data_norms[vcs->data_norm].fps_max;
1543
1544                 switch (fps) {
1545                 case 6:
1546                         mask = 0x003;
1547                         break;
1548                 case 12:
1549                         mask = 0x0c3;
1550                         break;
1551                 case 18:
1552                         mask = 0x333;
1553                         break;
1554                 case 24:
1555                         mask = 0x3ff;
1556                         break;
1557                 case 30:
1558                         mask = 0xfff;
1559                         break;
1560                 default:
1561                         mask = VINO_FRAMERT_FULL;
1562                 }
1563                 vcs->framert_reg = VINO_FRAMERT_RT(mask);
1564                 break;
1565         case VINO_DATA_NORM_PAL:
1566         case VINO_DATA_NORM_SECAM:
1567                 fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
1568
1569                 if (fps < vino_data_norms[vcs->data_norm].fps_min)
1570                         fps = vino_data_norms[vcs->data_norm].fps_min;
1571                 if (fps > vino_data_norms[vcs->data_norm].fps_max)
1572                         fps = vino_data_norms[vcs->data_norm].fps_max;
1573
1574                 switch (fps) {
1575                 case 5:
1576                         mask = 0x003;
1577                         break;
1578                 case 10:
1579                         mask = 0x0c3;
1580                         break;
1581                 case 15:
1582                         mask = 0x333;
1583                         break;
1584                 case 20:
1585                         mask = 0x0ff;
1586                         break;
1587                 case 25:
1588                         mask = 0x3ff;
1589                         break;
1590                 default:
1591                         mask = VINO_FRAMERT_FULL;
1592                 }
1593                 vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
1594                 break;
1595         }
1596
1597         vcs->fps = fps;
1598 }
1599
1600 /* execute with input_lock locked */
1601 static inline void vino_set_default_framerate(struct
1602                                               vino_channel_settings *vcs)
1603 {
1604         vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
1605 }
1606
1607 /* VINO I2C bus functions */
1608
1609 struct i2c_algo_sgi_data {
1610         void *data;     /* private data for lowlevel routines */
1611         unsigned (*getctrl)(void *data);
1612         void (*setctrl)(void *data, unsigned val);
1613         unsigned (*rdata)(void *data);
1614         void (*wdata)(void *data, unsigned val);
1615
1616         int xfer_timeout;
1617         int ack_timeout;
1618 };
1619
1620 static int wait_xfer_done(struct i2c_algo_sgi_data *adap)
1621 {
1622         int i;
1623
1624         for (i = 0; i < adap->xfer_timeout; i++) {
1625                 if ((adap->getctrl(adap->data) & SGI_I2C_XFER_BUSY) == 0)
1626                         return 0;
1627                 udelay(1);
1628         }
1629
1630         return -ETIMEDOUT;
1631 }
1632
1633 static int wait_ack(struct i2c_algo_sgi_data *adap)
1634 {
1635         int i;
1636
1637         if (wait_xfer_done(adap))
1638                 return -ETIMEDOUT;
1639         for (i = 0; i < adap->ack_timeout; i++) {
1640                 if ((adap->getctrl(adap->data) & SGI_I2C_NACK) == 0)
1641                         return 0;
1642                 udelay(1);
1643         }
1644
1645         return -ETIMEDOUT;
1646 }
1647
1648 static int force_idle(struct i2c_algo_sgi_data *adap)
1649 {
1650         int i;
1651
1652         adap->setctrl(adap->data, SGI_I2C_FORCE_IDLE);
1653         for (i = 0; i < adap->xfer_timeout; i++) {
1654                 if ((adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE) == 0)
1655                         goto out;
1656                 udelay(1);
1657         }
1658         return -ETIMEDOUT;
1659 out:
1660         if (adap->getctrl(adap->data) & SGI_I2C_BUS_ERR)
1661                 return -EIO;
1662         return 0;
1663 }
1664
1665 static int do_address(struct i2c_algo_sgi_data *adap, unsigned int addr,
1666                       int rd)
1667 {
1668         if (rd)
1669                 adap->setctrl(adap->data, SGI_I2C_NOT_IDLE);
1670         /* Check if bus is idle, eventually force it to do so */
1671         if (adap->getctrl(adap->data) & SGI_I2C_NOT_IDLE)
1672                 if (force_idle(adap))
1673                         return -EIO;
1674         /* Write out the i2c chip address and specify operation */
1675         adap->setctrl(adap->data,
1676                       SGI_I2C_HOLD_BUS | SGI_I2C_WRITE | SGI_I2C_NOT_IDLE);
1677         if (rd)
1678                 addr |= 1;
1679         adap->wdata(adap->data, addr);
1680         if (wait_ack(adap))
1681                 return -EIO;
1682         return 0;
1683 }
1684
1685 static int i2c_read(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1686                     unsigned int len)
1687 {
1688         int i;
1689
1690         adap->setctrl(adap->data,
1691                       SGI_I2C_HOLD_BUS | SGI_I2C_READ | SGI_I2C_NOT_IDLE);
1692         for (i = 0; i < len; i++) {
1693                 if (wait_xfer_done(adap))
1694                         return -EIO;
1695                 buf[i] = adap->rdata(adap->data);
1696         }
1697         adap->setctrl(adap->data, SGI_I2C_RELEASE_BUS | SGI_I2C_FORCE_IDLE);
1698
1699         return 0;
1700
1701 }
1702
1703 static int i2c_write(struct i2c_algo_sgi_data *adap, unsigned char *buf,
1704                      unsigned int len)
1705 {
1706         int i;
1707
1708         /* We are already in write state */
1709         for (i = 0; i < len; i++) {
1710                 adap->wdata(adap->data, buf[i]);
1711                 if (wait_ack(adap))
1712                         return -EIO;
1713         }
1714         return 0;
1715 }
1716
1717 static int sgi_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
1718                     int num)
1719 {
1720         struct i2c_algo_sgi_data *adap = i2c_adap->algo_data;
1721         struct i2c_msg *p;
1722         int i, err = 0;
1723
1724         for (i = 0; !err && i < num; i++) {
1725                 p = &msgs[i];
1726                 err = do_address(adap, p->addr, p->flags & I2C_M_RD);
1727                 if (err || !p->len)
1728                         continue;
1729                 if (p->flags & I2C_M_RD)
1730                         err = i2c_read(adap, p->buf, p->len);
1731                 else
1732                         err = i2c_write(adap, p->buf, p->len);
1733         }
1734
1735         return (err < 0) ? err : i;
1736 }
1737
1738 static u32 sgi_func(struct i2c_adapter *adap)
1739 {
1740         return I2C_FUNC_SMBUS_EMUL;
1741 }
1742
1743 static const struct i2c_algorithm sgi_algo = {
1744         .master_xfer    = sgi_xfer,
1745         .functionality  = sgi_func,
1746 };
1747
1748 static unsigned i2c_vino_getctrl(void *data)
1749 {
1750         return vino->i2c_control;
1751 }
1752
1753 static void i2c_vino_setctrl(void *data, unsigned val)
1754 {
1755         vino->i2c_control = val;
1756 }
1757
1758 static unsigned i2c_vino_rdata(void *data)
1759 {
1760         return vino->i2c_data;
1761 }
1762
1763 static void i2c_vino_wdata(void *data, unsigned val)
1764 {
1765         vino->i2c_data = val;
1766 }
1767
1768 static struct i2c_algo_sgi_data i2c_sgi_vino_data = {
1769         .getctrl = &i2c_vino_getctrl,
1770         .setctrl = &i2c_vino_setctrl,
1771         .rdata   = &i2c_vino_rdata,
1772         .wdata   = &i2c_vino_wdata,
1773         .xfer_timeout = 200,
1774         .ack_timeout  = 1000,
1775 };
1776
1777 static struct i2c_adapter vino_i2c_adapter = {
1778         .name                   = "VINO I2C bus",
1779         .id                     = I2C_HW_SGI_VINO,
1780         .algo                   = &sgi_algo,
1781         .algo_data              = &i2c_sgi_vino_data,
1782         .owner                  = THIS_MODULE,
1783 };
1784
1785 /*
1786  * Prepare VINO for DMA transfer...
1787  * (execute only with vino_lock and input_lock locked)
1788  */
1789 static int vino_dma_setup(struct vino_channel_settings *vcs,
1790                           struct vino_framebuffer *fb)
1791 {
1792         u32 ctrl, intr;
1793         struct sgi_vino_channel *ch;
1794         const struct vino_data_norm *norm;
1795
1796         dprintk("vino_dma_setup():\n");
1797
1798         vcs->field = 0;
1799         fb->frame_counter = 0;
1800
1801         ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1802         norm = &vino_data_norms[vcs->data_norm];
1803
1804         ch->page_index = 0;
1805         ch->line_count = 0;
1806
1807         /* VINO line size register is set 8 bytes less than actual */
1808         ch->line_size = vcs->line_size - 8;
1809
1810         /* let VINO know where to transfer data */
1811         ch->start_desc_tbl = fb->desc_table.dma;
1812         ch->next_4_desc = fb->desc_table.dma;
1813
1814         /* give vino time to fetch the first four descriptors, 5 usec
1815          * should be more than enough time */
1816         udelay(VINO_DESC_FETCH_DELAY);
1817
1818         dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
1819                 ch->start_desc_tbl, ch->next_4_desc);
1820
1821         /* set the alpha register */
1822         ch->alpha = vcs->alpha;
1823
1824         /* set clipping registers */
1825         ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
1826                 VINO_CLIP_EVEN(norm->even.top +
1827                                vcs->clipping.top / 2) |
1828                 VINO_CLIP_X(vcs->clipping.left);
1829         ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
1830                                      vcs->clipping.bottom / 2 - 1) |
1831                 VINO_CLIP_EVEN(norm->even.top +
1832                                vcs->clipping.bottom / 2 - 1) |
1833                 VINO_CLIP_X(vcs->clipping.right);
1834
1835         /* set the size of actual content in the buffer (DECIMATION !) */
1836         fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
1837                          vcs->decimation) *
1838                 ((vcs->clipping.bottom - vcs->clipping.top) /
1839                  vcs->decimation) *
1840                 vino_data_formats[vcs->data_format].bpp;
1841
1842         ch->frame_rate = vcs->framert_reg;
1843
1844         ctrl = vino->control;
1845         intr = vino->intr_status;
1846
1847         if (vcs->channel == VINO_CHANNEL_A) {
1848                 /* All interrupt conditions for this channel was cleared
1849                  * so clear the interrupt status register and enable
1850                  * interrupts */
1851                 intr &= ~VINO_INTSTAT_A;
1852                 ctrl |= VINO_CTRL_A_INT;
1853
1854                 /* enable synchronization */
1855                 ctrl |= VINO_CTRL_A_SYNC_ENBL;
1856
1857                 /* enable frame assembly */
1858                 ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
1859
1860                 /* set decimation used */
1861                 if (vcs->decimation < 2)
1862                         ctrl &= ~VINO_CTRL_A_DEC_ENBL;
1863                 else {
1864                         ctrl |= VINO_CTRL_A_DEC_ENBL;
1865                         ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
1866                         ctrl |= (vcs->decimation - 1) <<
1867                                 VINO_CTRL_A_DEC_SCALE_SHIFT;
1868                 }
1869
1870                 /* select input interface */
1871                 if (vcs->input == VINO_INPUT_D1)
1872                         ctrl |= VINO_CTRL_A_SELECT;
1873                 else
1874                         ctrl &= ~VINO_CTRL_A_SELECT;
1875
1876                 /* palette */
1877                 ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
1878                           VINO_CTRL_A_DITHER);
1879         } else {
1880                 intr &= ~VINO_INTSTAT_B;
1881                 ctrl |= VINO_CTRL_B_INT;
1882
1883                 ctrl |= VINO_CTRL_B_SYNC_ENBL;
1884                 ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
1885
1886                 if (vcs->decimation < 2)
1887                         ctrl &= ~VINO_CTRL_B_DEC_ENBL;
1888                 else {
1889                         ctrl |= VINO_CTRL_B_DEC_ENBL;
1890                         ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
1891                         ctrl |= (vcs->decimation - 1) <<
1892                                 VINO_CTRL_B_DEC_SCALE_SHIFT;
1893
1894                 }
1895                 if (vcs->input == VINO_INPUT_D1)
1896                         ctrl |= VINO_CTRL_B_SELECT;
1897                 else
1898                         ctrl &= ~VINO_CTRL_B_SELECT;
1899
1900                 ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
1901                           VINO_CTRL_B_DITHER);
1902         }
1903
1904         /* set palette */
1905         fb->data_format = vcs->data_format;
1906
1907         switch (vcs->data_format) {
1908                 case VINO_DATA_FMT_GREY:
1909                         ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1910                                 VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
1911                         break;
1912                 case VINO_DATA_FMT_RGB32:
1913                         ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1914                                 VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
1915                         break;
1916                 case VINO_DATA_FMT_YUV:
1917                         /* nothing needs to be done */
1918                         break;
1919                 case VINO_DATA_FMT_RGB332:
1920                         ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1921                                 VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
1922                                 VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
1923                         break;
1924         }
1925
1926         vino->intr_status = intr;
1927         vino->control = ctrl;
1928
1929         return 0;
1930 }
1931
1932 /* (execute only with vino_lock locked) */
1933 static inline void vino_dma_start(struct vino_channel_settings *vcs)
1934 {
1935         u32 ctrl = vino->control;
1936
1937         dprintk("vino_dma_start():\n");
1938         ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
1939                 VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
1940         vino->control = ctrl;
1941 }
1942
1943 /* (execute only with vino_lock locked) */
1944 static inline void vino_dma_stop(struct vino_channel_settings *vcs)
1945 {
1946         u32 ctrl = vino->control;
1947
1948         ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1949                 ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
1950         ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
1951                 ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
1952         vino->control = ctrl;
1953         dprintk("vino_dma_stop():\n");
1954 }
1955
1956 /*
1957  * Load dummy page to descriptor registers. This prevents generating of
1958  * spurious interrupts. (execute only with vino_lock locked)
1959  */
1960 static void vino_clear_interrupt(struct vino_channel_settings *vcs)
1961 {
1962         struct sgi_vino_channel *ch;
1963
1964         ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
1965
1966         ch->page_index = 0;
1967         ch->line_count = 0;
1968
1969         ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
1970         ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
1971
1972         udelay(VINO_DESC_FETCH_DELAY);
1973         dprintk("channel %c clear interrupt condition\n",
1974                (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
1975 }
1976
1977 static int vino_capture(struct vino_channel_settings *vcs,
1978                         struct vino_framebuffer *fb)
1979 {
1980         int err = 0;
1981         unsigned long flags, flags2;
1982
1983         spin_lock_irqsave(&fb->state_lock, flags);
1984
1985         if (fb->state == VINO_FRAMEBUFFER_IN_USE)
1986                 err = -EBUSY;
1987         fb->state = VINO_FRAMEBUFFER_IN_USE;
1988
1989         spin_unlock_irqrestore(&fb->state_lock, flags);
1990
1991         if (err)
1992                 return err;
1993
1994         spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
1995         spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
1996
1997         vino_dma_setup(vcs, fb);
1998         vino_dma_start(vcs);
1999
2000         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
2001         spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2002
2003         return err;
2004 }
2005
2006 static
2007 struct vino_framebuffer *vino_capture_enqueue(struct
2008                                               vino_channel_settings *vcs,
2009                                               unsigned int index)
2010 {
2011         struct vino_framebuffer *fb;
2012         unsigned long flags;
2013
2014         dprintk("vino_capture_enqueue():\n");
2015
2016         spin_lock_irqsave(&vcs->capture_lock, flags);
2017
2018         fb = vino_queue_add(&vcs->fb_queue, index);
2019         if (fb == NULL) {
2020                 dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
2021                         "queue full?\n");
2022                 goto out;
2023         }
2024 out:
2025         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2026
2027         return fb;
2028 }
2029
2030 static int vino_capture_next(struct vino_channel_settings *vcs, int start)
2031 {
2032         struct vino_framebuffer *fb;
2033         unsigned int incoming, id;
2034         int err = 0;
2035         unsigned long flags;
2036
2037         dprintk("vino_capture_next():\n");
2038
2039         spin_lock_irqsave(&vcs->capture_lock, flags);
2040
2041         if (start) {
2042                 /* start capture only if capture isn't in progress already */
2043                 if (vcs->capturing) {
2044                         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2045                         return 0;
2046                 }
2047
2048         } else {
2049                 /* capture next frame:
2050                  * stop capture if capturing is not set */
2051                 if (!vcs->capturing) {
2052                         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2053                         return 0;
2054                 }
2055         }
2056
2057         err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
2058         if (err) {
2059                 dprintk("vino_capture_next(): vino_queue_get_incoming() "
2060                         "failed\n");
2061                 err = -EINVAL;
2062                 goto out;
2063         }
2064         if (incoming == 0) {
2065                 dprintk("vino_capture_next(): no buffers available\n");
2066                 goto out;
2067         }
2068
2069         fb = vino_queue_peek(&vcs->fb_queue, &id);
2070         if (fb == NULL) {
2071                 dprintk("vino_capture_next(): vino_queue_peek() failed\n");
2072                 err = -EINVAL;
2073                 goto out;
2074         }
2075
2076         if (start) {
2077                 vcs->capturing = 1;
2078         }
2079
2080         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2081
2082         err = vino_capture(vcs, fb);
2083
2084         return err;
2085
2086 out:
2087         vcs->capturing = 0;
2088         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2089
2090         return err;
2091 }
2092
2093 static inline int vino_is_capturing(struct vino_channel_settings *vcs)
2094 {
2095         int ret;
2096         unsigned long flags;
2097
2098         spin_lock_irqsave(&vcs->capture_lock, flags);
2099
2100         ret = vcs->capturing;
2101
2102         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2103
2104         return ret;
2105 }
2106
2107 /* waits until a frame is captured */
2108 static int vino_wait_for_frame(struct vino_channel_settings *vcs)
2109 {
2110         wait_queue_t wait;
2111         int err = 0;
2112
2113         dprintk("vino_wait_for_frame():\n");
2114
2115         init_waitqueue_entry(&wait, current);
2116         /* add ourselves into wait queue */
2117         add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2118
2119         /* to ensure that schedule_timeout will return immediately
2120          * if VINO interrupt was triggered meanwhile */
2121         schedule_timeout_interruptible(msecs_to_jiffies(100));
2122
2123         if (signal_pending(current))
2124                 err = -EINTR;
2125
2126         remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
2127
2128         dprintk("vino_wait_for_frame(): waiting for frame %s\n",
2129                 err ? "failed" : "ok");
2130
2131         return err;
2132 }
2133
2134 /* the function assumes that PAGE_SIZE % 4 == 0 */
2135 static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
2136         unsigned char *pageptr;
2137         unsigned int page, i;
2138         unsigned char a;
2139
2140         for (page = 0; page < fb->desc_table.page_count; page++) {
2141                 pageptr = (unsigned char *)fb->desc_table.virtual[page];
2142
2143                 for (i = 0; i < PAGE_SIZE; i += 4) {
2144                         a = pageptr[0];
2145                         pageptr[0] = pageptr[3];
2146                         pageptr[1] = pageptr[2];
2147                         pageptr[2] = pageptr[1];
2148                         pageptr[3] = a;
2149                         pageptr += 4;
2150                 }
2151         }
2152 }
2153
2154 /* checks if the buffer is in correct state and syncs data */
2155 static int vino_check_buffer(struct vino_channel_settings *vcs,
2156                              struct vino_framebuffer *fb)
2157 {
2158         int err = 0;
2159         unsigned long flags;
2160
2161         dprintk("vino_check_buffer():\n");
2162
2163         spin_lock_irqsave(&fb->state_lock, flags);
2164         switch (fb->state) {
2165         case VINO_FRAMEBUFFER_IN_USE:
2166                 err = -EIO;
2167                 break;
2168         case VINO_FRAMEBUFFER_READY:
2169                 vino_sync_buffer(fb);
2170                 fb->state = VINO_FRAMEBUFFER_UNUSED;
2171                 break;
2172         default:
2173                 err = -EINVAL;
2174         }
2175         spin_unlock_irqrestore(&fb->state_lock, flags);
2176
2177         if (!err) {
2178                 if (vino_pixel_conversion
2179                     && (fb->data_format == VINO_DATA_FMT_RGB32)) {
2180                         vino_convert_to_rgba(fb);
2181                 }
2182         } else if (err && (err != -EINVAL)) {
2183                 dprintk("vino_check_buffer(): buffer not ready\n");
2184
2185                 spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2186                 vino_dma_stop(vcs);
2187                 vino_clear_interrupt(vcs);
2188                 spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2189         }
2190
2191         return err;
2192 }
2193
2194 /* forcefully terminates capture */
2195 static void vino_capture_stop(struct vino_channel_settings *vcs)
2196 {
2197         unsigned int incoming = 0, outgoing = 0, id;
2198         unsigned long flags, flags2;
2199
2200         dprintk("vino_capture_stop():\n");
2201
2202         spin_lock_irqsave(&vcs->capture_lock, flags);
2203
2204         /* unset capturing to stop queue processing */
2205         vcs->capturing = 0;
2206
2207         spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
2208
2209         vino_dma_stop(vcs);
2210         vino_clear_interrupt(vcs);
2211
2212         spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
2213
2214         /* remove all items from the queue */
2215         if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2216                 dprintk("vino_capture_stop(): "
2217                         "vino_queue_get_incoming() failed\n");
2218                 goto out;
2219         }
2220         while (incoming > 0) {
2221                 vino_queue_transfer(&vcs->fb_queue);
2222
2223                 if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
2224                         dprintk("vino_capture_stop(): "
2225                                 "vino_queue_get_incoming() failed\n");
2226                         goto out;
2227                 }
2228         }
2229
2230         if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2231                 dprintk("vino_capture_stop(): "
2232                         "vino_queue_get_outgoing() failed\n");
2233                 goto out;
2234         }
2235         while (outgoing > 0) {
2236                 vino_queue_remove(&vcs->fb_queue, &id);
2237
2238                 if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
2239                         dprintk("vino_capture_stop(): "
2240                                 "vino_queue_get_outgoing() failed\n");
2241                         goto out;
2242                 }
2243         }
2244
2245 out:
2246         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2247 }
2248
2249 #if 0
2250 static int vino_capture_failed(struct vino_channel_settings *vcs)
2251 {
2252         struct vino_framebuffer *fb;
2253         unsigned long flags;
2254         unsigned int i;
2255         int ret;
2256
2257         dprintk("vino_capture_failed():\n");
2258
2259         spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
2260
2261         vino_dma_stop(vcs);
2262         vino_clear_interrupt(vcs);
2263
2264         spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
2265
2266         ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
2267         if (ret == VINO_QUEUE_ERROR) {
2268                 dprintk("vino_queue_get_incoming() failed\n");
2269                 return -EINVAL;
2270         }
2271         if (i == 0) {
2272                 /* no buffers to process */
2273                 return 0;
2274         }
2275
2276         fb = vino_queue_peek(&vcs->fb_queue, &i);
2277         if (fb == NULL) {
2278                 dprintk("vino_queue_peek() failed\n");
2279                 return -EINVAL;
2280         }
2281
2282         spin_lock_irqsave(&fb->state_lock, flags);
2283         if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
2284                 fb->state = VINO_FRAMEBUFFER_UNUSED;
2285                 vino_queue_transfer(&vcs->fb_queue);
2286                 vino_queue_remove(&vcs->fb_queue, &i);
2287                 /* we should actually discard the newest frame,
2288                  * but who cares ... */
2289         }
2290         spin_unlock_irqrestore(&fb->state_lock, flags);
2291
2292         return 0;
2293 }
2294 #endif
2295
2296 static void vino_skip_frame(struct vino_channel_settings *vcs)
2297 {
2298         struct vino_framebuffer *fb;
2299         unsigned long flags;
2300         unsigned int id;
2301
2302         spin_lock_irqsave(&vcs->capture_lock, flags);
2303         fb = vino_queue_peek(&vcs->fb_queue, &id);
2304         if (!fb) {
2305                 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2306                 dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
2307                 return;
2308         }
2309         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2310
2311         spin_lock_irqsave(&fb->state_lock, flags);
2312         fb->state = VINO_FRAMEBUFFER_UNUSED;
2313         spin_unlock_irqrestore(&fb->state_lock, flags);
2314
2315         vino_capture_next(vcs, 0);
2316 }
2317
2318 static void vino_frame_done(struct vino_channel_settings *vcs)
2319 {
2320         struct vino_framebuffer *fb;
2321         unsigned long flags;
2322
2323         spin_lock_irqsave(&vcs->capture_lock, flags);
2324         fb = vino_queue_transfer(&vcs->fb_queue);
2325         if (!fb) {
2326                 spin_unlock_irqrestore(&vcs->capture_lock, flags);
2327                 dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
2328                 return;
2329         }
2330         spin_unlock_irqrestore(&vcs->capture_lock, flags);
2331
2332         fb->frame_counter = vcs->int_data.frame_counter;
2333         memcpy(&fb->timestamp, &vcs->int_data.timestamp,
2334                sizeof(struct timeval));
2335
2336         spin_lock_irqsave(&fb->state_lock, flags);
2337         if (fb->state == VINO_FRAMEBUFFER_IN_USE)
2338                 fb->state = VINO_FRAMEBUFFER_READY;
2339         spin_unlock_irqrestore(&fb->state_lock, flags);
2340
2341         wake_up(&vcs->fb_queue.frame_wait_queue);
2342
2343         vino_capture_next(vcs, 0);
2344 }
2345
2346 static void vino_capture_tasklet(unsigned long channel) {
2347         struct vino_channel_settings *vcs;
2348
2349         vcs = (channel == VINO_CHANNEL_A)
2350                 ? &vino_drvdata->a : &vino_drvdata->b;
2351
2352         if (vcs->int_data.skip)
2353                 vcs->int_data.skip_count++;
2354
2355         if (vcs->int_data.skip && (vcs->int_data.skip_count
2356                                    <= VINO_MAX_FRAME_SKIP_COUNT)) {
2357                 vino_skip_frame(vcs);
2358         } else {
2359                 vcs->int_data.skip_count = 0;
2360                 vino_frame_done(vcs);
2361         }
2362 }
2363
2364 static irqreturn_t vino_interrupt(int irq, void *dev_id)
2365 {
2366         u32 ctrl, intr;
2367         unsigned int fc_a, fc_b;
2368         int handled_a = 0, skip_a = 0, done_a = 0;
2369         int handled_b = 0, skip_b = 0, done_b = 0;
2370
2371 #ifdef VINO_DEBUG_INT
2372         int loop = 0;
2373         unsigned int line_count = vino->a.line_count,
2374                 page_index = vino->a.page_index,
2375                 field_counter = vino->a.field_counter,
2376                 start_desc_tbl = vino->a.start_desc_tbl,
2377                 next_4_desc = vino->a.next_4_desc;
2378         unsigned int line_count_2,
2379                 page_index_2,
2380                 field_counter_2,
2381                 start_desc_tbl_2,
2382                 next_4_desc_2;
2383 #endif
2384
2385         spin_lock(&vino_drvdata->vino_lock);
2386
2387         while ((intr = vino->intr_status)) {
2388                 fc_a = vino->a.field_counter >> 1;
2389                 fc_b = vino->b.field_counter >> 1;
2390
2391                 /* handle error-interrupts in some special way ?
2392                  * --> skips frames */
2393                 if (intr & VINO_INTSTAT_A) {
2394                         if (intr & VINO_INTSTAT_A_EOF) {
2395                                 vino_drvdata->a.field++;
2396                                 if (vino_drvdata->a.field > 1) {
2397                                         vino_dma_stop(&vino_drvdata->a);
2398                                         vino_clear_interrupt(&vino_drvdata->a);
2399                                         vino_drvdata->a.field = 0;
2400                                         done_a = 1;
2401                                 } else {
2402                                         if (vino->a.page_index
2403                                             != vino_drvdata->a.line_size) {
2404                                                 vino->a.line_count = 0;
2405                                                 vino->a.page_index =
2406                                                         vino_drvdata->
2407                                                         a.line_size;
2408                                                 vino->a.next_4_desc =
2409                                                         vino->a.start_desc_tbl;
2410                                         }
2411                                 }
2412                                 dprintk("channel A end-of-field "
2413                                         "interrupt: %04x\n", intr);
2414                         } else {
2415                                 vino_dma_stop(&vino_drvdata->a);
2416                                 vino_clear_interrupt(&vino_drvdata->a);
2417                                 vino_drvdata->a.field = 0;
2418                                 skip_a = 1;
2419                                 dprintk("channel A error interrupt: %04x\n",
2420                                         intr);
2421                         }
2422
2423 #ifdef VINO_DEBUG_INT
2424                         line_count_2 = vino->a.line_count;
2425                         page_index_2 = vino->a.page_index;
2426                         field_counter_2 = vino->a.field_counter;
2427                         start_desc_tbl_2 = vino->a.start_desc_tbl;
2428                         next_4_desc_2 = vino->a.next_4_desc;
2429
2430                         printk("intr = %04x, loop = %d, field = %d\n",
2431                                intr, loop, vino_drvdata->a.field);
2432                         printk("1- line count = %04d, page index = %04d, "
2433                                "start = %08x, next = %08x\n"
2434                                "   fieldc = %d, framec = %d\n",
2435                                line_count, page_index, start_desc_tbl,
2436                                next_4_desc, field_counter, fc_a);
2437                         printk("12-line count = %04d, page index = %04d, "
2438                                "   start = %08x, next = %08x\n",
2439                                line_count_2, page_index_2, start_desc_tbl_2,
2440                                next_4_desc_2);
2441
2442                         if (done_a)
2443                                 printk("\n");
2444 #endif
2445                 }
2446
2447                 if (intr & VINO_INTSTAT_B) {
2448                         if (intr & VINO_INTSTAT_B_EOF) {
2449                                 vino_drvdata->b.field++;
2450                                 if (vino_drvdata->b.field > 1) {
2451                                         vino_dma_stop(&vino_drvdata->b);
2452                                         vino_clear_interrupt(&vino_drvdata->b);
2453                                         vino_drvdata->b.field = 0;
2454                                         done_b = 1;
2455                                 }
2456                                 dprintk("channel B end-of-field "
2457                                         "interrupt: %04x\n", intr);
2458                         } else {
2459                                 vino_dma_stop(&vino_drvdata->b);
2460                                 vino_clear_interrupt(&vino_drvdata->b);
2461                                 vino_drvdata->b.field = 0;
2462                                 skip_b = 1;
2463                                 dprintk("channel B error interrupt: %04x\n",
2464                                         intr);
2465                         }
2466                 }
2467
2468                 /* Always remember to clear interrupt status.
2469                  * Disable VINO interrupts while we do this. */
2470                 ctrl = vino->control;
2471                 vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
2472                 vino->intr_status = ~intr;
2473                 vino->control = ctrl;
2474
2475                 spin_unlock(&vino_drvdata->vino_lock);
2476
2477                 if ((!handled_a) && (done_a || skip_a)) {
2478                         if (!skip_a) {
2479                                 do_gettimeofday(&vino_drvdata->
2480                                                 a.int_data.timestamp);
2481                                 vino_drvdata->a.int_data.frame_counter = fc_a;
2482                         }
2483                         vino_drvdata->a.int_data.skip = skip_a;
2484
2485                         dprintk("channel A %s, interrupt: %d\n",
2486                                 skip_a ? "skipping frame" : "frame done",
2487                                 intr);
2488                         tasklet_hi_schedule(&vino_tasklet_a);
2489                         handled_a = 1;
2490                 }
2491
2492                 if ((!handled_b) && (done_b || skip_b)) {
2493                         if (!skip_b) {
2494                                 do_gettimeofday(&vino_drvdata->
2495                                                 b.int_data.timestamp);
2496                                 vino_drvdata->b.int_data.frame_counter = fc_b;
2497                         }
2498                         vino_drvdata->b.int_data.skip = skip_b;
2499
2500                         dprintk("channel B %s, interrupt: %d\n",
2501                                 skip_b ? "skipping frame" : "frame done",
2502                                 intr);
2503                         tasklet_hi_schedule(&vino_tasklet_b);
2504                         handled_b = 1;
2505                 }
2506
2507 #ifdef VINO_DEBUG_INT
2508                 loop++;
2509 #endif
2510                 spin_lock(&vino_drvdata->vino_lock);
2511         }
2512
2513         spin_unlock(&vino_drvdata->vino_lock);
2514
2515         return IRQ_HANDLED;
2516 }
2517
2518 /* VINO video input management */
2519
2520 static int vino_get_saa7191_input(int input)
2521 {
2522         switch (input) {
2523         case VINO_INPUT_COMPOSITE:
2524                 return SAA7191_INPUT_COMPOSITE;
2525         case VINO_INPUT_SVIDEO:
2526                 return SAA7191_INPUT_SVIDEO;
2527         default:
2528                 printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
2529                        "invalid input!\n");
2530                 return -1;
2531         }
2532 }
2533
2534 /* execute with input_lock locked */
2535 static int vino_is_input_owner(struct vino_channel_settings *vcs)
2536 {
2537         switch(vcs->input) {
2538         case VINO_INPUT_COMPOSITE:
2539         case VINO_INPUT_SVIDEO:
2540                 return vino_drvdata->decoder_owner == vcs->channel;
2541         case VINO_INPUT_D1:
2542                 return vino_drvdata->camera_owner == vcs->channel;
2543         default:
2544                 return 0;
2545         }
2546 }
2547
2548 static int vino_acquire_input(struct vino_channel_settings *vcs)
2549 {
2550         unsigned long flags;
2551         int ret = 0;
2552
2553         dprintk("vino_acquire_input():\n");
2554
2555         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2556
2557         /* First try D1 and then SAA7191 */
2558         if (vino_drvdata->camera
2559             && (vino_drvdata->camera_owner == VINO_NO_CHANNEL)) {
2560                 vino_drvdata->camera_owner = vcs->channel;
2561                 vcs->input = VINO_INPUT_D1;
2562                 vcs->data_norm = VINO_DATA_NORM_D1;
2563         } else if (vino_drvdata->decoder
2564                    && (vino_drvdata->decoder_owner == VINO_NO_CHANNEL)) {
2565                 int input;
2566                 int data_norm;
2567                 v4l2_std_id norm;
2568                 struct v4l2_routing route = { 0, 0 };
2569
2570                 input = VINO_INPUT_COMPOSITE;
2571
2572                 route.input = vino_get_saa7191_input(input);
2573                 ret = decoder_call(video, s_routing, &route);
2574                 if (ret) {
2575                         ret = -EINVAL;
2576                         goto out;
2577                 }
2578
2579                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2580
2581                 /* Don't hold spinlocks while auto-detecting norm
2582                  * as it may take a while... */
2583
2584                 ret = decoder_call(video, querystd, &norm);
2585                 if (!ret) {
2586                         for (data_norm = 0; data_norm < 3; data_norm++) {
2587                                 if (vino_data_norms[data_norm].std & norm)
2588                                         break;
2589                         }
2590                         if (data_norm == 3)
2591                                 data_norm = VINO_DATA_NORM_PAL;
2592                         ret = decoder_call(tuner, s_std, norm);
2593                 }
2594
2595                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2596
2597                 if (ret) {
2598                         ret = -EINVAL;
2599                         goto out;
2600                 }
2601
2602                 vino_drvdata->decoder_owner = vcs->channel;
2603
2604                 vcs->input = input;
2605                 vcs->data_norm = data_norm;
2606         } else {
2607                 vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
2608                         vino_drvdata->b.input : vino_drvdata->a.input;
2609                 vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
2610                         vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
2611         }
2612
2613         if (vcs->input == VINO_INPUT_NONE) {
2614                 ret = -ENODEV;
2615                 goto out;
2616         }
2617
2618         vino_set_default_clipping(vcs);
2619         vino_set_default_scaling(vcs);
2620         vino_set_default_framerate(vcs);
2621
2622         dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
2623
2624 out:
2625         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2626
2627         return ret;
2628 }
2629
2630 static int vino_set_input(struct vino_channel_settings *vcs, int input)
2631 {
2632         struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2633                 &vino_drvdata->b : &vino_drvdata->a;
2634         unsigned long flags;
2635         int ret = 0;
2636
2637         dprintk("vino_set_input():\n");
2638
2639         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2640
2641         if (vcs->input == input)
2642                 goto out;
2643
2644         switch (input) {
2645         case VINO_INPUT_COMPOSITE:
2646         case VINO_INPUT_SVIDEO:
2647                 if (!vino_drvdata->decoder) {
2648                         ret = -EINVAL;
2649                         goto out;
2650                 }
2651
2652                 if (vino_drvdata->decoder_owner == VINO_NO_CHANNEL) {
2653                         vino_drvdata->decoder_owner = vcs->channel;
2654                 }
2655
2656                 if (vino_drvdata->decoder_owner == vcs->channel) {
2657                         int data_norm;
2658                         v4l2_std_id norm;
2659                         struct v4l2_routing route = { 0, 0 };
2660
2661                         route.input = vino_get_saa7191_input(input);
2662                         ret = decoder_call(video, s_routing, &route);
2663                         if (ret) {
2664                                 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2665                                 ret = -EINVAL;
2666                                 goto out;
2667                         }
2668
2669                         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2670
2671                         /* Don't hold spinlocks while auto-detecting norm
2672                          * as it may take a while... */
2673
2674                         ret = decoder_call(video, querystd, &norm);
2675                         if (!ret) {
2676                                 for (data_norm = 0; data_norm < 3; data_norm++) {
2677                                         if (vino_data_norms[data_norm].std & norm)
2678                                                 break;
2679                                 }
2680                                 if (data_norm == 3)
2681                                         data_norm = VINO_DATA_NORM_PAL;
2682                                 ret = decoder_call(tuner, s_std, norm);
2683                         }
2684
2685                         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2686
2687                         if (ret) {
2688                                 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2689                                 ret = -EINVAL;
2690                                 goto out;
2691                         }
2692
2693                         vcs->input = input;
2694                         vcs->data_norm = data_norm;
2695                 } else {
2696                         if (input != vcs2->input) {
2697                                 ret = -EBUSY;
2698                                 goto out;
2699                         }
2700
2701                         vcs->input = input;
2702                         vcs->data_norm = vcs2->data_norm;
2703                 }
2704
2705                 if (vino_drvdata->camera_owner == vcs->channel) {
2706                         /* Transfer the ownership or release the input */
2707                         if (vcs2->input == VINO_INPUT_D1) {
2708                                 vino_drvdata->camera_owner = vcs2->channel;
2709                         } else {
2710                                 vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2711                         }
2712                 }
2713                 break;
2714         case VINO_INPUT_D1:
2715                 if (!vino_drvdata->camera) {
2716                         ret = -EINVAL;
2717                         goto out;
2718                 }
2719
2720                 if (vino_drvdata->camera_owner == VINO_NO_CHANNEL)
2721                         vino_drvdata->camera_owner = vcs->channel;
2722
2723                 if (vino_drvdata->decoder_owner == vcs->channel) {
2724                         /* Transfer the ownership or release the input */
2725                         if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2726                                  (vcs2->input == VINO_INPUT_SVIDEO)) {
2727                                 vino_drvdata->decoder_owner = vcs2->channel;
2728                         } else {
2729                                 vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2730                         }
2731                 }
2732
2733                 vcs->input = input;
2734                 vcs->data_norm = VINO_DATA_NORM_D1;
2735                 break;
2736         default:
2737                 ret = -EINVAL;
2738                 goto out;
2739         }
2740
2741         vino_set_default_clipping(vcs);
2742         vino_set_default_scaling(vcs);
2743         vino_set_default_framerate(vcs);
2744
2745         dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
2746
2747 out:
2748         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2749
2750         return ret;
2751 }
2752
2753 static void vino_release_input(struct vino_channel_settings *vcs)
2754 {
2755         struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
2756                 &vino_drvdata->b : &vino_drvdata->a;
2757         unsigned long flags;
2758
2759         dprintk("vino_release_input():\n");
2760
2761         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2762
2763         /* Release ownership of the channel
2764          * and if the other channel takes input from
2765          * the same source, transfer the ownership */
2766         if (vino_drvdata->camera_owner == vcs->channel) {
2767                 if (vcs2->input == VINO_INPUT_D1) {
2768                         vino_drvdata->camera_owner = vcs2->channel;
2769                 } else {
2770                         vino_drvdata->camera_owner = VINO_NO_CHANNEL;
2771                 }
2772         } else if (vino_drvdata->decoder_owner == vcs->channel) {
2773                 if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
2774                          (vcs2->input == VINO_INPUT_SVIDEO)) {
2775                         vino_drvdata->decoder_owner = vcs2->channel;
2776                 } else {
2777                         vino_drvdata->decoder_owner = VINO_NO_CHANNEL;
2778                 }
2779         }
2780         vcs->input = VINO_INPUT_NONE;
2781
2782         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2783 }
2784
2785 /* execute with input_lock locked */
2786 static int vino_set_data_norm(struct vino_channel_settings *vcs,
2787                               unsigned int data_norm,
2788                               unsigned long *flags)
2789 {
2790         int err = 0;
2791
2792         if (data_norm == vcs->data_norm)
2793                 return 0;
2794
2795         switch (vcs->input) {
2796         case VINO_INPUT_D1:
2797                 /* only one "norm" supported */
2798                 if (data_norm != VINO_DATA_NORM_D1)
2799                         return -EINVAL;
2800                 break;
2801         case VINO_INPUT_COMPOSITE:
2802         case VINO_INPUT_SVIDEO: {
2803                 v4l2_std_id norm;
2804
2805                 if ((data_norm != VINO_DATA_NORM_PAL)
2806                     && (data_norm != VINO_DATA_NORM_NTSC)
2807                     && (data_norm != VINO_DATA_NORM_SECAM))
2808                         return -EINVAL;
2809
2810                 spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
2811
2812                 /* Don't hold spinlocks while setting norm
2813                  * as it may take a while... */
2814
2815                 norm = vino_data_norms[data_norm].std;
2816                 err = decoder_call(tuner, s_std, norm);
2817
2818                 spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
2819
2820                 if (err)
2821                         goto out;
2822
2823                 vcs->data_norm = data_norm;
2824
2825                 vino_set_default_clipping(vcs);
2826                 vino_set_default_scaling(vcs);
2827                 vino_set_default_framerate(vcs);
2828                 break;
2829         }
2830         default:
2831                 return -EINVAL;
2832         }
2833
2834 out:
2835         return err;
2836 }
2837
2838 /* V4L2 helper functions */
2839
2840 static int vino_find_data_format(__u32 pixelformat)
2841 {
2842         int i;
2843
2844         for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
2845                 if (vino_data_formats[i].pixelformat == pixelformat)
2846                         return i;
2847         }
2848
2849         return VINO_DATA_FMT_NONE;
2850 }
2851
2852 static int vino_int_enum_input(struct vino_channel_settings *vcs, __u32 index)
2853 {
2854         int input = VINO_INPUT_NONE;
2855         unsigned long flags;
2856
2857         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2858         if (vino_drvdata->decoder && vino_drvdata->camera) {
2859                 switch (index) {
2860                 case 0:
2861                         input = VINO_INPUT_COMPOSITE;
2862                         break;
2863                 case 1:
2864                         input = VINO_INPUT_SVIDEO;
2865                         break;
2866                 case 2:
2867                         input = VINO_INPUT_D1;
2868                         break;
2869                 }
2870         } else if (vino_drvdata->decoder) {
2871                 switch (index) {
2872                 case 0:
2873                         input = VINO_INPUT_COMPOSITE;
2874                         break;
2875                 case 1:
2876                         input = VINO_INPUT_SVIDEO;
2877                         break;
2878                 }
2879         } else if (vino_drvdata->camera) {
2880                 switch (index) {
2881                 case 0:
2882                         input = VINO_INPUT_D1;
2883                         break;
2884                 }
2885         }
2886         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2887
2888         return input;
2889 }
2890
2891 /* execute with input_lock locked */
2892 static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
2893 {
2894         __u32 index = 0;
2895         // FIXME: detect when no inputs available
2896
2897         if (vino_drvdata->decoder && vino_drvdata->camera) {
2898                 switch (vcs->input) {
2899                 case VINO_INPUT_COMPOSITE:
2900                         index = 0;
2901                         break;
2902                 case VINO_INPUT_SVIDEO:
2903                         index = 1;
2904                         break;
2905                 case VINO_INPUT_D1:
2906                         index = 2;
2907                         break;
2908                 }
2909         } else if (vino_drvdata->decoder) {
2910                 switch (vcs->input) {
2911                 case VINO_INPUT_COMPOSITE:
2912                         index = 0;
2913                         break;
2914                 case VINO_INPUT_SVIDEO:
2915                         index = 1;
2916                         break;
2917                 }
2918         } else if (vino_drvdata->camera) {
2919                 switch (vcs->input) {
2920                 case VINO_INPUT_D1:
2921                         index = 0;
2922                         break;
2923                 }
2924         }
2925
2926         return index;
2927 }
2928
2929 /* V4L2 ioctls */
2930
2931 static int vino_querycap(struct file *file, void *__fh,
2932                 struct v4l2_capability *cap)
2933 {
2934         memset(cap, 0, sizeof(struct v4l2_capability));
2935
2936         strcpy(cap->driver, vino_driver_name);
2937         strcpy(cap->card, vino_driver_description);
2938         strcpy(cap->bus_info, vino_bus_name);
2939         cap->version = VINO_VERSION_CODE;
2940         cap->capabilities =
2941                 V4L2_CAP_VIDEO_CAPTURE |
2942                 V4L2_CAP_STREAMING;
2943         // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
2944         return 0;
2945 }
2946
2947 static int vino_enum_input(struct file *file, void *__fh,
2948                                struct v4l2_input *i)
2949 {
2950         struct vino_channel_settings *vcs = video_drvdata(file);
2951         __u32 index = i->index;
2952         int input;
2953         dprintk("requested index = %d\n", index);
2954
2955         input = vino_int_enum_input(vcs, index);
2956         if (input == VINO_INPUT_NONE)
2957                 return -EINVAL;
2958
2959         memset(i, 0, sizeof(struct v4l2_input));
2960
2961         i->index = index;
2962         i->type = V4L2_INPUT_TYPE_CAMERA;
2963         i->std = vino_inputs[input].std;
2964         strcpy(i->name, vino_inputs[input].name);
2965
2966         if (input == VINO_INPUT_COMPOSITE || input == VINO_INPUT_SVIDEO)
2967                 decoder_call(video, g_input_status, &i->status);
2968         return 0;
2969 }
2970
2971 static int vino_g_input(struct file *file, void *__fh,
2972                              unsigned int *i)
2973 {
2974         struct vino_channel_settings *vcs = video_drvdata(file);
2975         __u32 index;
2976         int input;
2977         unsigned long flags;
2978
2979         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
2980         input = vcs->input;
2981         index = vino_find_input_index(vcs);
2982         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
2983
2984         dprintk("input = %d\n", input);
2985
2986         if (input == VINO_INPUT_NONE) {
2987                 return -EINVAL;
2988         }
2989
2990         *i = index;
2991
2992         return 0;
2993 }
2994
2995 static int vino_s_input(struct file *file, void *__fh,
2996                              unsigned int i)
2997 {
2998         struct vino_channel_settings *vcs = video_drvdata(file);
2999         int input;
3000         dprintk("requested input = %d\n", i);
3001
3002         input = vino_int_enum_input(vcs, i);
3003         if (input == VINO_INPUT_NONE)
3004                 return -EINVAL;
3005
3006         return vino_set_input(vcs, input);
3007 }
3008
3009 static int vino_querystd(struct file *file, void *__fh,
3010                               v4l2_std_id *std)
3011 {
3012         struct vino_channel_settings *vcs = video_drvdata(file);
3013         unsigned long flags;
3014         int err = 0;
3015
3016         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3017
3018         switch (vcs->input) {
3019         case VINO_INPUT_D1:
3020                 *std = vino_inputs[vcs->input].std;
3021                 break;
3022         case VINO_INPUT_COMPOSITE:
3023         case VINO_INPUT_SVIDEO: {
3024                 decoder_call(video, querystd, std);
3025                 break;
3026         }
3027         default:
3028                 err = -EINVAL;
3029         }
3030
3031         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3032
3033         return err;
3034 }
3035
3036 static int vino_g_std(struct file *file, void *__fh,
3037                            v4l2_std_id *std)
3038 {
3039         struct vino_channel_settings *vcs = video_drvdata(file);
3040         unsigned long flags;
3041
3042         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3043
3044         *std = vino_data_norms[vcs->data_norm].std;
3045         dprintk("current standard = %d\n", vcs->data_norm);
3046
3047         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3048
3049         return 0;
3050 }
3051
3052 static int vino_s_std(struct file *file, void *__fh,
3053                            v4l2_std_id *std)
3054 {
3055         struct vino_channel_settings *vcs = video_drvdata(file);
3056         unsigned long flags;
3057         int ret = 0;
3058
3059         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3060
3061         if (!vino_is_input_owner(vcs)) {
3062                 ret = -EBUSY;
3063                 goto out;
3064         }
3065
3066         /* check if the standard is valid for the current input */
3067         if ((*std) & vino_inputs[vcs->input].std) {
3068                 dprintk("standard accepted\n");
3069
3070                 /* change the video norm for SAA7191
3071                  * and accept NTSC for D1 (do nothing) */
3072
3073                 if (vcs->input == VINO_INPUT_D1)
3074                         goto out;
3075
3076                 if ((*std) & V4L2_STD_PAL) {
3077                         ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
3078                                                  &flags);
3079                 } else if ((*std) & V4L2_STD_NTSC) {
3080                         ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
3081                                                  &flags);
3082                 } else if ((*std) & V4L2_STD_SECAM) {
3083                         ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
3084                                                  &flags);
3085                 } else {
3086                         ret = -EINVAL;
3087                 }
3088
3089                 if (ret) {
3090                         ret = -EINVAL;
3091                 }
3092         } else {
3093                 ret = -EINVAL;
3094         }
3095
3096 out:
3097         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3098
3099         return ret;
3100 }
3101
3102 static int vino_enum_fmt_vid_cap(struct file *file, void *__fh,
3103                               struct v4l2_fmtdesc *fd)
3104 {
3105         enum v4l2_buf_type type = fd->type;
3106         int index = fd->index;
3107
3108         dprintk("format index = %d\n", index);
3109
3110         if ((fd->index < 0) ||
3111                         (fd->index >= VINO_DATA_FMT_COUNT))
3112                 return -EINVAL;
3113         dprintk("format name = %s\n",
3114                         vino_data_formats[index].description);
3115
3116         memset(fd, 0, sizeof(struct v4l2_fmtdesc));
3117         fd->index = index;
3118         fd->type = type;
3119         fd->pixelformat = vino_data_formats[index].pixelformat;
3120         strcpy(fd->description, vino_data_formats[index].description);
3121         return 0;
3122 }
3123
3124 static int vino_try_fmt_vid_cap(struct file *file, void *__fh,
3125                              struct v4l2_format *f)
3126 {
3127         struct vino_channel_settings *vcs = video_drvdata(file);
3128         struct vino_channel_settings tempvcs;
3129         unsigned long flags;
3130         struct v4l2_pix_format *pf = &f->fmt.pix;
3131
3132         dprintk("requested: w = %d, h = %d\n",
3133                         pf->width, pf->height);
3134
3135         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3136         memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
3137         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3138
3139         tempvcs.data_format = vino_find_data_format(pf->pixelformat);
3140         if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
3141                 tempvcs.data_format = VINO_DATA_FMT_GREY;
3142                 pf->pixelformat =
3143                         vino_data_formats[tempvcs.data_format].
3144                         pixelformat;
3145         }
3146
3147         /* data format must be set before clipping/scaling */
3148         vino_set_scaling(&tempvcs, pf->width, pf->height);
3149
3150         dprintk("data format = %s\n",
3151                         vino_data_formats[tempvcs.data_format].description);
3152
3153         pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
3154                 tempvcs.decimation;
3155         pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3156                 tempvcs.decimation;
3157
3158         pf->field = V4L2_FIELD_INTERLACED;
3159         pf->bytesperline = tempvcs.line_size;
3160         pf->sizeimage = tempvcs.line_size *
3161                 (tempvcs.clipping.bottom - tempvcs.clipping.top) /
3162                 tempvcs.decimation;
3163         pf->colorspace =
3164                 vino_data_formats[tempvcs.data_format].colorspace;
3165
3166         pf->priv = 0;
3167         return 0;
3168 }
3169
3170 static int vino_g_fmt_vid_cap(struct file *file, void *__fh,
3171                            struct v4l2_format *f)
3172 {
3173         struct vino_channel_settings *vcs = video_drvdata(file);
3174         unsigned long flags;
3175         struct v4l2_pix_format *pf = &f->fmt.pix;
3176
3177         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3178
3179         pf->width = (vcs->clipping.right - vcs->clipping.left) /
3180                 vcs->decimation;
3181         pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
3182                 vcs->decimation;
3183         pf->pixelformat =
3184                 vino_data_formats[vcs->data_format].pixelformat;
3185
3186         pf->field = V4L2_FIELD_INTERLACED;
3187         pf->bytesperline = vcs->line_size;
3188         pf->sizeimage = vcs->line_size *
3189                 (vcs->clipping.bottom - vcs->clipping.top) /
3190                 vcs->decimation;
3191         pf->colorspace =
3192                 vino_data_formats[vcs->data_format].colorspace;
3193
3194         pf->priv = 0;
3195
3196         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3197         return 0;
3198 }
3199
3200 static int vino_s_fmt_vid_cap(struct file *file, void *__fh,
3201                            struct v4l2_format *f)
3202 {
3203         struct vino_channel_settings *vcs = video_drvdata(file);
3204         int data_format;
3205         unsigned long flags;
3206         struct v4l2_pix_format *pf = &f->fmt.pix;
3207
3208         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3209
3210         data_format = vino_find_data_format(pf->pixelformat);
3211
3212         if (data_format == VINO_DATA_FMT_NONE) {
3213                 vcs->data_format = VINO_DATA_FMT_GREY;
3214                 pf->pixelformat =
3215                         vino_data_formats[vcs->data_format].
3216                         pixelformat;
3217         } else {
3218                 vcs->data_format = data_format;
3219         }
3220
3221         /* data format must be set before clipping/scaling */
3222         vino_set_scaling(vcs, pf->width, pf->height);
3223
3224         dprintk("data format = %s\n",
3225                vino_data_formats[vcs->data_format].description);
3226
3227         pf->width = vcs->clipping.right - vcs->clipping.left;
3228         pf->height = vcs->clipping.bottom - vcs->clipping.top;
3229
3230         pf->field = V4L2_FIELD_INTERLACED;
3231         pf->bytesperline = vcs->line_size;
3232         pf->sizeimage = vcs->line_size *
3233                 (vcs->clipping.bottom - vcs->clipping.top) /
3234                 vcs->decimation;
3235         pf->colorspace =
3236                 vino_data_formats[vcs->data_format].colorspace;
3237
3238         pf->priv = 0;
3239
3240         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3241         return 0;
3242 }
3243
3244 static int vino_cropcap(struct file *file, void *__fh,
3245                              struct v4l2_cropcap *ccap)
3246 {
3247         struct vino_channel_settings *vcs = video_drvdata(file);
3248         const struct vino_data_norm *norm;
3249         unsigned long flags;
3250
3251         switch (ccap->type) {
3252         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3253                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3254
3255                 norm = &vino_data_norms[vcs->data_norm];
3256
3257                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3258
3259                 ccap->bounds.left = 0;
3260                 ccap->bounds.top = 0;
3261                 ccap->bounds.width = norm->width;
3262                 ccap->bounds.height = norm->height;
3263                 memcpy(&ccap->defrect, &ccap->bounds,
3264                        sizeof(struct v4l2_rect));
3265
3266                 ccap->pixelaspect.numerator = 1;
3267                 ccap->pixelaspect.denominator = 1;
3268                 break;
3269         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3270         default:
3271                 return -EINVAL;
3272         }
3273
3274         return 0;
3275 }
3276
3277 static int vino_g_crop(struct file *file, void *__fh,
3278                             struct v4l2_crop *c)
3279 {
3280         struct vino_channel_settings *vcs = video_drvdata(file);
3281         unsigned long flags;
3282
3283         switch (c->type) {
3284         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3285                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3286
3287                 c->c.left = vcs->clipping.left;
3288                 c->c.top = vcs->clipping.top;
3289                 c->c.width = vcs->clipping.right - vcs->clipping.left;
3290                 c->c.height = vcs->clipping.bottom - vcs->clipping.top;
3291
3292                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3293                 break;
3294         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3295         default:
3296                 return -EINVAL;
3297         }
3298
3299         return 0;
3300 }
3301
3302 static int vino_s_crop(struct file *file, void *__fh,
3303                             struct v4l2_crop *c)
3304 {
3305         struct vino_channel_settings *vcs = video_drvdata(file);
3306         unsigned long flags;
3307
3308         switch (c->type) {
3309         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3310                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3311
3312                 vino_set_clipping(vcs, c->c.left, c->c.top,
3313                                   c->c.width, c->c.height);
3314
3315                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3316                 break;
3317         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3318         default:
3319                 return -EINVAL;
3320         }
3321
3322         return 0;
3323 }
3324
3325 static int vino_g_parm(struct file *file, void *__fh,
3326                             struct v4l2_streamparm *sp)
3327 {
3328         struct vino_channel_settings *vcs = video_drvdata(file);
3329         unsigned long flags;
3330
3331         switch (sp->type) {
3332         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3333                 struct v4l2_captureparm *cp = &sp->parm.capture;
3334                 memset(cp, 0, sizeof(struct v4l2_captureparm));
3335
3336                 cp->capability = V4L2_CAP_TIMEPERFRAME;
3337                 cp->timeperframe.numerator = 1;
3338
3339                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3340
3341                 cp->timeperframe.denominator = vcs->fps;
3342
3343                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3344
3345                 // TODO: cp->readbuffers = xxx;
3346                 break;
3347         }
3348         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3349         default:
3350                 return -EINVAL;
3351         }
3352
3353         return 0;
3354 }
3355
3356 static int vino_s_parm(struct file *file, void *__fh,
3357                             struct v4l2_streamparm *sp)
3358 {
3359         struct vino_channel_settings *vcs = video_drvdata(file);
3360         unsigned long flags;
3361
3362         switch (sp->type) {
3363         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3364                 struct v4l2_captureparm *cp = &sp->parm.capture;
3365
3366                 spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3367
3368                 if ((cp->timeperframe.numerator == 0) ||
3369                     (cp->timeperframe.denominator == 0)) {
3370                         /* reset framerate */
3371                         vino_set_default_framerate(vcs);
3372                 } else {
3373                         vino_set_framerate(vcs, cp->timeperframe.denominator /
3374                                            cp->timeperframe.numerator);
3375                 }
3376
3377                 spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3378
3379                 // TODO: set buffers according to cp->readbuffers
3380                 break;
3381         }
3382         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3383         default:
3384                 return -EINVAL;
3385         }
3386
3387         return 0;
3388 }
3389
3390 static int vino_reqbufs(struct file *file, void *__fh,
3391                              struct v4l2_requestbuffers *rb)
3392 {
3393         struct vino_channel_settings *vcs = video_drvdata(file);
3394         if (vcs->reading)
3395                 return -EBUSY;
3396
3397         switch (rb->type) {
3398         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3399                 // TODO: check queue type
3400                 if (rb->memory != V4L2_MEMORY_MMAP) {
3401                         dprintk("type not mmap\n");
3402                         return -EINVAL;
3403                 }
3404
3405                 dprintk("count = %d\n", rb->count);
3406                 if (rb->count > 0) {
3407                         if (vino_is_capturing(vcs)) {
3408                                 dprintk("busy, capturing\n");
3409                                 return -EBUSY;
3410                         }
3411
3412                         if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
3413                                 dprintk("busy, buffers still mapped\n");
3414                                 return -EBUSY;
3415                         } else {
3416                                 vcs->streaming = 0;
3417                                 vino_queue_free(&vcs->fb_queue);
3418                                 vino_queue_init(&vcs->fb_queue, &rb->count);
3419                         }
3420                 } else {
3421                         vcs->streaming = 0;
3422                         vino_capture_stop(vcs);
3423                         vino_queue_free(&vcs->fb_queue);
3424                 }
3425                 break;
3426         }
3427         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3428         default:
3429                 return -EINVAL;
3430         }
3431
3432         return 0;
3433 }
3434
3435 static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
3436                                         struct vino_framebuffer *fb,
3437                                         struct v4l2_buffer *b)
3438 {
3439         if (vino_queue_outgoing_contains(&vcs->fb_queue,
3440                                          fb->id)) {
3441                 b->flags &= ~V4L2_BUF_FLAG_QUEUED;
3442                 b->flags |= V4L2_BUF_FLAG_DONE;
3443         } else if (vino_queue_incoming_contains(&vcs->fb_queue,
3444                                        fb->id)) {
3445                 b->flags &= ~V4L2_BUF_FLAG_DONE;
3446                 b->flags |= V4L2_BUF_FLAG_QUEUED;
3447         } else {
3448                 b->flags &= ~(V4L2_BUF_FLAG_DONE |
3449                               V4L2_BUF_FLAG_QUEUED);
3450         }
3451
3452         b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
3453
3454         if (fb->map_count > 0)
3455                 b->flags |= V4L2_BUF_FLAG_MAPPED;
3456
3457         b->index = fb->id;
3458         b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
3459                 V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
3460         b->m.offset = fb->offset;
3461         b->bytesused = fb->data_size;
3462         b->length = fb->size;
3463         b->field = V4L2_FIELD_INTERLACED;
3464         b->sequence = fb->frame_counter;
3465         memcpy(&b->timestamp, &fb->timestamp,
3466                sizeof(struct timeval));
3467         // b->input ?
3468
3469         dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
3470                 fb->id, fb->size, fb->data_size, fb->offset);
3471 }
3472
3473 static int vino_querybuf(struct file *file, void *__fh,
3474                               struct v4l2_buffer *b)
3475 {
3476         struct vino_channel_settings *vcs = video_drvdata(file);
3477         if (vcs->reading)
3478                 return -EBUSY;
3479
3480         switch (b->type) {
3481         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3482                 struct vino_framebuffer *fb;
3483
3484                 // TODO: check queue type
3485                 if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
3486                         dprintk("invalid index = %d\n",
3487                                b->index);
3488                         return -EINVAL;
3489                 }
3490
3491                 fb = vino_queue_get_buffer(&vcs->fb_queue,
3492                                            b->index);
3493                 if (fb == NULL) {
3494                         dprintk("vino_queue_get_buffer() failed");
3495                         return -EINVAL;
3496                 }
3497
3498                 vino_v4l2_get_buffer_status(vcs, fb, b);
3499                 break;
3500         }
3501         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3502         default:
3503                 return -EINVAL;
3504         }
3505
3506         return 0;
3507 }
3508
3509 static int vino_qbuf(struct file *file, void *__fh,
3510                           struct v4l2_buffer *b)
3511 {
3512         struct vino_channel_settings *vcs = video_drvdata(file);
3513         if (vcs->reading)
3514                 return -EBUSY;
3515
3516         switch (b->type) {
3517         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3518                 struct vino_framebuffer *fb;
3519                 int ret;
3520
3521                 // TODO: check queue type
3522                 if (b->memory != V4L2_MEMORY_MMAP) {
3523                         dprintk("type not mmap\n");
3524                         return -EINVAL;
3525                 }
3526
3527                 fb = vino_capture_enqueue(vcs, b->index);
3528                 if (fb == NULL)
3529                         return -EINVAL;
3530
3531                 vino_v4l2_get_buffer_status(vcs, fb, b);
3532
3533                 if (vcs->streaming) {
3534                         ret = vino_capture_next(vcs, 1);
3535                         if (ret)
3536                                 return ret;
3537                 }
3538                 break;
3539         }
3540         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3541         default:
3542                 return -EINVAL;
3543         }
3544
3545         return 0;
3546 }
3547
3548 static int vino_dqbuf(struct file *file, void *__fh,
3549                            struct v4l2_buffer *b)
3550 {
3551         struct vino_channel_settings *vcs = video_drvdata(file);
3552         unsigned int nonblocking = file->f_flags & O_NONBLOCK;
3553         if (vcs->reading)
3554                 return -EBUSY;
3555
3556         switch (b->type) {
3557         case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
3558                 struct vino_framebuffer *fb;
3559                 unsigned int incoming, outgoing;
3560                 int err;
3561
3562                 // TODO: check queue type
3563
3564                 err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3565                 if (err) {
3566                         dprintk("vino_queue_get_incoming() failed\n");
3567                         return -EINVAL;
3568                 }
3569                 err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
3570                 if (err) {
3571                         dprintk("vino_queue_get_outgoing() failed\n");
3572                         return -EINVAL;
3573                 }
3574
3575                 dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
3576
3577                 if (outgoing == 0) {
3578                         if (incoming == 0) {
3579                                 dprintk("no incoming or outgoing buffers\n");
3580                                 return -EINVAL;
3581                         }
3582                         if (nonblocking) {
3583                                 dprintk("non-blocking I/O was selected and "
3584                                         "there are no buffers to dequeue\n");
3585                                 return -EAGAIN;
3586                         }
3587
3588                         err = vino_wait_for_frame(vcs);
3589                         if (err) {
3590                                 err = vino_wait_for_frame(vcs);
3591                                 if (err) {
3592                                         /* interrupted or
3593                                          * no frames captured because
3594                                          * of frame skipping */
3595                                         // vino_capture_failed(vcs);
3596                                         return -EIO;
3597                                 }
3598                         }
3599                 }
3600
3601                 fb = vino_queue_remove(&vcs->fb_queue, &b->index);
3602                 if (fb == NULL) {
3603                         dprintk("vino_queue_remove() failed\n");
3604                         return -EINVAL;
3605                 }
3606
3607                 err = vino_check_buffer(vcs, fb);
3608
3609                 vino_v4l2_get_buffer_status(vcs, fb, b);
3610
3611                 if (err)
3612                         return -EIO;
3613
3614                 break;
3615         }
3616         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3617         default:
3618                 return -EINVAL;
3619         }
3620
3621         return 0;
3622 }
3623
3624 static int vino_streamon(struct file *file, void *__fh,
3625                 enum v4l2_buf_type i)
3626 {
3627         struct vino_channel_settings *vcs = video_drvdata(file);
3628         unsigned int incoming;
3629         int ret;
3630         if (vcs->reading)
3631                 return -EBUSY;
3632
3633         if (vcs->streaming)
3634                 return 0;
3635
3636         // TODO: check queue type
3637
3638         if (vino_queue_get_length(&vcs->fb_queue) < 1) {
3639                 dprintk("no buffers allocated\n");
3640                 return -EINVAL;
3641         }
3642
3643         ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
3644         if (ret) {
3645                 dprintk("vino_queue_get_incoming() failed\n");
3646                 return -EINVAL;
3647         }
3648
3649         vcs->streaming = 1;
3650
3651         if (incoming > 0) {
3652                 ret = vino_capture_next(vcs, 1);
3653                 if (ret) {
3654                         vcs->streaming = 0;
3655
3656                         dprintk("couldn't start capture\n");
3657                         return -EINVAL;
3658                 }
3659         }
3660
3661         return 0;
3662 }
3663
3664 static int vino_streamoff(struct file *file, void *__fh,
3665                 enum v4l2_buf_type i)
3666 {
3667         struct vino_channel_settings *vcs = video_drvdata(file);
3668         if (vcs->reading)
3669                 return -EBUSY;
3670
3671         if (!vcs->streaming)
3672                 return 0;
3673
3674         vcs->streaming = 0;
3675         vino_capture_stop(vcs);
3676
3677         return 0;
3678 }
3679
3680 static int vino_queryctrl(struct file *file, void *__fh,
3681                                struct v4l2_queryctrl *queryctrl)
3682 {
3683         struct vino_channel_settings *vcs = video_drvdata(file);
3684         unsigned long flags;
3685         int i;
3686         int err = 0;
3687
3688         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3689
3690         switch (vcs->input) {
3691         case VINO_INPUT_D1:
3692                 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3693                         if (vino_indycam_v4l2_controls[i].id ==
3694                             queryctrl->id) {
3695                                 memcpy(queryctrl,
3696                                        &vino_indycam_v4l2_controls[i],
3697                                        sizeof(struct v4l2_queryctrl));
3698                                 queryctrl->reserved[0] = 0;
3699                                 goto found;
3700                         }
3701                 }
3702
3703                 err =  -EINVAL;
3704                 break;
3705         case VINO_INPUT_COMPOSITE:
3706         case VINO_INPUT_SVIDEO:
3707                 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3708                         if (vino_saa7191_v4l2_controls[i].id ==
3709                             queryctrl->id) {
3710                                 memcpy(queryctrl,
3711                                        &vino_saa7191_v4l2_controls[i],
3712                                        sizeof(struct v4l2_queryctrl));
3713                                 queryctrl->reserved[0] = 0;
3714                                 goto found;
3715                         }
3716                 }
3717
3718                 err =  -EINVAL;
3719                 break;
3720         default:
3721                 err =  -EINVAL;
3722         }
3723
3724  found:
3725         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3726
3727         return err;
3728 }
3729
3730 static int vino_g_ctrl(struct file *file, void *__fh,
3731                             struct v4l2_control *control)
3732 {
3733         struct vino_channel_settings *vcs = video_drvdata(file);
3734         unsigned long flags;
3735         int i;
3736         int err = 0;
3737
3738         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3739
3740         switch (vcs->input) {
3741         case VINO_INPUT_D1: {
3742                 err = -EINVAL;
3743                 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3744                         if (vino_indycam_v4l2_controls[i].id == control->id) {
3745                                 err = 0;
3746                                 break;
3747                         }
3748                 }
3749
3750                 if (err)
3751                         goto out;
3752
3753                 err = camera_call(core, g_ctrl, control);
3754                 if (err)
3755                         err = -EINVAL;
3756                 break;
3757         }
3758         case VINO_INPUT_COMPOSITE:
3759         case VINO_INPUT_SVIDEO: {
3760                 err = -EINVAL;
3761                 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3762                         if (vino_saa7191_v4l2_controls[i].id == control->id) {
3763                                 err = 0;
3764                                 break;
3765                         }
3766                 }
3767
3768                 if (err)
3769                         goto out;
3770
3771                 err = decoder_call(core, g_ctrl, control);
3772                 if (err)
3773                         err = -EINVAL;
3774                 break;
3775         }
3776         default:
3777                 err =  -EINVAL;
3778         }
3779
3780 out:
3781         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3782
3783         return err;
3784 }
3785
3786 static int vino_s_ctrl(struct file *file, void *__fh,
3787                             struct v4l2_control *control)
3788 {
3789         struct vino_channel_settings *vcs = video_drvdata(file);
3790         unsigned long flags;
3791         int i;
3792         int err = 0;
3793
3794         spin_lock_irqsave(&vino_drvdata->input_lock, flags);
3795
3796         if (!vino_is_input_owner(vcs)) {
3797                 err = -EBUSY;
3798                 goto out;
3799         }
3800
3801         switch (vcs->input) {
3802         case VINO_INPUT_D1: {
3803                 err = -EINVAL;
3804                 for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
3805                         if (vino_indycam_v4l2_controls[i].id == control->id) {
3806                                 err = 0;
3807                                 break;
3808                         }
3809                 }
3810                 if (err)
3811                         goto out;
3812                 if (control->value < vino_indycam_v4l2_controls[i].minimum ||
3813                     control->value > vino_indycam_v4l2_controls[i].maximum) {
3814                         err = -ERANGE;
3815                         goto out;
3816                 }
3817                 err = camera_call(core, s_ctrl, control);
3818                 if (err)
3819                         err = -EINVAL;
3820                 break;
3821         }
3822         case VINO_INPUT_COMPOSITE:
3823         case VINO_INPUT_SVIDEO: {
3824                 err = -EINVAL;
3825                 for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
3826                         if (vino_saa7191_v4l2_controls[i].id == control->id) {
3827                                 err = 0;
3828                                 break;
3829                         }
3830                 }
3831                 if (err)
3832                         goto out;
3833                 if (control->value < vino_saa7191_v4l2_controls[i].minimum ||
3834                     control->value > vino_saa7191_v4l2_controls[i].maximum) {
3835                         err = -ERANGE;
3836                         goto out;
3837                 }
3838
3839                 err = decoder_call(core, s_ctrl, control);
3840                 if (err)
3841                         err = -EINVAL;
3842                 break;
3843         }
3844         default:
3845                 err =  -EINVAL;
3846         }
3847
3848 out:
3849         spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
3850
3851         return err;
3852 }
3853
3854 /* File operations */
3855
3856 static int vino_open(struct file *file)
3857 {
3858         struct vino_channel_settings *vcs = video_drvdata(file);
3859         int ret = 0;
3860         dprintk("open(): channel = %c\n",
3861                (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
3862
3863         mutex_lock(&vcs->mutex);
3864
3865         if (vcs->users) {
3866                 dprintk("open(): driver busy\n");
3867                 ret = -EBUSY;
3868                 goto out;
3869         }
3870
3871         ret = vino_acquire_input(vcs);
3872         if (ret) {
3873                 dprintk("open(): vino_acquire_input() failed\n");
3874                 goto out;
3875         }
3876
3877         vcs->users++;
3878
3879  out:
3880         mutex_unlock(&vcs->mutex);
3881
3882         dprintk("open(): %s!\n", ret ? "failed" : "complete");
3883
3884         return ret;
3885 }
3886
3887 static int vino_close(struct file *file)
3888 {
3889         struct vino_channel_settings *vcs = video_drvdata(file);
3890         dprintk("close():\n");
3891
3892         mutex_lock(&vcs->mutex);
3893
3894         vcs->users--;
3895
3896         if (!vcs->users) {
3897                 vino_release_input(vcs);
3898
3899                 /* stop DMA and free buffers */
3900                 vino_capture_stop(vcs);
3901                 vino_queue_free(&vcs->fb_queue);
3902         }
3903
3904         mutex_unlock(&vcs->mutex);
3905
3906         return 0;
3907 }
3908
3909 static void vino_vm_open(struct vm_area_struct *vma)
3910 {
3911         struct vino_framebuffer *fb = vma->vm_private_data;
3912
3913         fb->map_count++;
3914         dprintk("vino_vm_open(): count = %d\n", fb->map_count);
3915 }
3916
3917 static void vino_vm_close(struct vm_area_struct *vma)
3918 {
3919         struct vino_framebuffer *fb = vma->vm_private_data;
3920
3921         fb->map_count--;
3922         dprintk("vino_vm_close(): count = %d\n", fb->map_count);
3923 }
3924
3925 static struct vm_operations_struct vino_vm_ops = {
3926         .open   = vino_vm_open,
3927         .close  = vino_vm_close,
3928 };
3929
3930 static int vino_mmap(struct file *file, struct vm_area_struct *vma)
3931 {
3932         struct vino_channel_settings *vcs = video_drvdata(file);
3933
3934         unsigned long start = vma->vm_start;
3935         unsigned long size = vma->vm_end - vma->vm_start;
3936         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
3937
3938         struct vino_framebuffer *fb = NULL;
3939         unsigned int i, length;
3940         int ret = 0;
3941
3942         dprintk("mmap():\n");
3943
3944         // TODO: reject mmap if already mapped
3945
3946         if (mutex_lock_interruptible(&vcs->mutex))
3947                 return -EINTR;
3948
3949         if (vcs->reading) {
3950                 ret = -EBUSY;
3951                 goto out;
3952         }
3953
3954         // TODO: check queue type
3955
3956         if (!(vma->vm_flags & VM_WRITE)) {
3957                 dprintk("mmap(): app bug: PROT_WRITE please\n");
3958                 ret = -EINVAL;
3959                 goto out;
3960         }
3961         if (!(vma->vm_flags & VM_SHARED)) {
3962                 dprintk("mmap(): app bug: MAP_SHARED please\n");
3963                 ret = -EINVAL;
3964                 goto out;
3965         }
3966
3967         /* find the correct buffer using offset */
3968         length = vino_queue_get_length(&vcs->fb_queue);
3969         if (length == 0) {
3970                 dprintk("mmap(): queue not initialized\n");
3971                 ret = -EINVAL;
3972                 goto out;
3973         }
3974
3975         for (i = 0; i < length; i++) {
3976                 fb = vino_queue_get_buffer(&vcs->fb_queue, i);
3977                 if (fb == NULL) {
3978                         dprintk("mmap(): vino_queue_get_buffer() failed\n");
3979                         ret = -EINVAL;
3980                         goto out;
3981                 }
3982
3983                 if (fb->offset == offset)
3984                         goto found;
3985         }
3986
3987         dprintk("mmap(): invalid offset = %lu\n", offset);
3988         ret = -EINVAL;
3989         goto out;
3990
3991 found:
3992         dprintk("mmap(): buffer = %d\n", i);
3993
3994         if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
3995                 dprintk("mmap(): failed: size = %lu > %lu\n",
3996                         size, fb->desc_table.page_count * PAGE_SIZE);
3997                 ret = -EINVAL;
3998                 goto out;
3999         }
4000
4001         for (i = 0; i < fb->desc_table.page_count; i++) {
4002                 unsigned long pfn =
4003                         virt_to_phys((void *)fb->desc_table.virtual[i]) >>
4004                         PAGE_SHIFT;
4005
4006                 if (size < PAGE_SIZE)
4007                         break;
4008
4009                 // protection was: PAGE_READONLY
4010                 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
4011                                     vma->vm_page_prot)) {
4012                         dprintk("mmap(): remap_pfn_range() failed\n");
4013                         ret = -EAGAIN;
4014                         goto out;
4015                 }
4016
4017                 start += PAGE_SIZE;
4018                 size -= PAGE_SIZE;
4019         }
4020
4021         fb->map_count = 1;
4022
4023         vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
4024         vma->vm_flags &= ~VM_IO;
4025         vma->vm_private_data = fb;
4026         vma->vm_file = file;
4027         vma->vm_ops = &vino_vm_ops;
4028
4029 out:
4030         mutex_unlock(&vcs->mutex);
4031
4032         return ret;
4033 }
4034
4035 static unsigned int vino_poll(struct file *file, poll_table *pt)
4036 {
4037         struct vino_channel_settings *vcs = video_drvdata(file);
4038         unsigned int outgoing;
4039         unsigned int ret = 0;
4040
4041         // lock mutex (?)
4042         // TODO: this has to be corrected for different read modes
4043
4044         dprintk("poll():\n");
4045
4046         if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4047                 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4048                 ret = POLLERR;
4049                 goto error;
4050         }
4051         if (outgoing > 0)
4052                 goto over;
4053
4054         poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
4055
4056         if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
4057                 dprintk("poll(): vino_queue_get_outgoing() failed\n");
4058                 ret = POLLERR;
4059                 goto error;
4060         }
4061
4062 over:
4063         dprintk("poll(): data %savailable\n",
4064                 (outgoing > 0) ? "" : "not ");
4065
4066         if (outgoing > 0)
4067                 ret = POLLIN | POLLRDNORM;
4068
4069 error:
4070         return ret;
4071 }
4072
4073 static long vino_ioctl(struct file *file,
4074                       unsigned int cmd, unsigned long arg)
4075 {
4076         struct vino_channel_settings *vcs = video_drvdata(file);
4077         long ret;
4078
4079         if (mutex_lock_interruptible(&vcs->mutex))
4080                 return -EINTR;
4081
4082         ret = video_ioctl2(file, cmd, arg);
4083
4084         mutex_unlock(&vcs->mutex);
4085
4086         return ret;
4087 }
4088
4089 /* Initialization and cleanup */
4090
4091 /* __initdata */
4092 static int vino_init_stage;
4093
4094 const struct v4l2_ioctl_ops vino_ioctl_ops = {
4095         .vidioc_enum_fmt_vid_cap     = vino_enum_fmt_vid_cap,
4096         .vidioc_g_fmt_vid_cap        = vino_g_fmt_vid_cap,
4097         .vidioc_s_fmt_vid_cap        = vino_s_fmt_vid_cap,
4098         .vidioc_try_fmt_vid_cap      = vino_try_fmt_vid_cap,
4099         .vidioc_querycap             = vino_querycap,
4100         .vidioc_enum_input           = vino_enum_input,
4101         .vidioc_g_input              = vino_g_input,
4102         .vidioc_s_input              = vino_s_input,
4103         .vidioc_g_std                = vino_g_std,
4104         .vidioc_s_std                = vino_s_std,
4105         .vidioc_querystd             = vino_querystd,
4106         .vidioc_cropcap              = vino_cropcap,
4107         .vidioc_s_crop               = vino_s_crop,
4108         .vidioc_g_crop               = vino_g_crop,
4109         .vidioc_s_parm               = vino_s_parm,
4110         .vidioc_g_parm               = vino_g_parm,
4111         .vidioc_reqbufs              = vino_reqbufs,
4112         .vidioc_querybuf             = vino_querybuf,
4113         .vidioc_qbuf                 = vino_qbuf,
4114         .vidioc_dqbuf                = vino_dqbuf,
4115         .vidioc_streamon             = vino_streamon,
4116         .vidioc_streamoff            = vino_streamoff,
4117         .vidioc_queryctrl            = vino_queryctrl,
4118         .vidioc_g_ctrl               = vino_g_ctrl,
4119         .vidioc_s_ctrl               = vino_s_ctrl,
4120 };
4121
4122 static const struct v4l2_file_operations vino_fops = {
4123         .owner          = THIS_MODULE,
4124         .open           = vino_open,
4125         .release        = vino_close,
4126         .unlocked_ioctl = vino_ioctl,
4127         .mmap           = vino_mmap,
4128         .poll           = vino_poll,
4129 };
4130
4131 static struct video_device vdev_template = {
4132         .name           = "NOT SET",
4133         .fops           = &vino_fops,
4134         .ioctl_ops      = &vino_ioctl_ops,
4135         .tvnorms        = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
4136         .minor          = -1,
4137 };
4138
4139 static void vino_module_cleanup(int stage)
4140 {
4141         switch(stage) {
4142         case 11:
4143                 video_unregister_device(vino_drvdata->b.vdev);
4144                 vino_drvdata->b.vdev = NULL;
4145         case 10:
4146                 video_unregister_device(vino_drvdata->a.vdev);
4147                 vino_drvdata->a.vdev = NULL;
4148         case 9:
4149                 i2c_del_adapter(&vino_i2c_adapter);
4150         case 8:
4151                 free_irq(SGI_VINO_IRQ, NULL);
4152         case 7:
4153                 if (vino_drvdata->b.vdev) {
4154                         video_device_release(vino_drvdata->b.vdev);
4155                         vino_drvdata->b.vdev = NULL;
4156                 }
4157         case 6:
4158                 if (vino_drvdata->a.vdev) {
4159                         video_device_release(vino_drvdata->a.vdev);
4160                         vino_drvdata->a.vdev = NULL;
4161                 }
4162         case 5:
4163                 /* all entries in dma_cpu dummy table have the same address */
4164                 dma_unmap_single(NULL,
4165                                  vino_drvdata->dummy_desc_table.dma_cpu[0],
4166                                  PAGE_SIZE, DMA_FROM_DEVICE);
4167                 dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
4168                                   * sizeof(dma_addr_t),
4169                                   (void *)vino_drvdata->
4170                                   dummy_desc_table.dma_cpu,
4171                                   vino_drvdata->dummy_desc_table.dma);
4172         case 4:
4173                 free_page(vino_drvdata->dummy_page);
4174         case 3:
4175                 v4l2_device_unregister(&vino_drvdata->v4l2_dev);
4176         case 2:
4177                 kfree(vino_drvdata);
4178         case 1:
4179                 iounmap(vino);
4180         case 0:
4181                 break;
4182         default:
4183                 dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
4184                         stage);
4185         }
4186 }
4187
4188 static int vino_probe(void)
4189 {
4190         unsigned long rev_id;
4191
4192         if (ip22_is_fullhouse()) {
4193                 printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
4194                 return -ENODEV;
4195         }
4196
4197         if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
4198                 printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
4199                 return -ENODEV;
4200         }
4201
4202         vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
4203         if (!vino) {
4204                 printk(KERN_ERR "VINO: ioremap() failed\n");
4205                 return -EIO;
4206         }
4207         vino_init_stage++;
4208
4209         if (get_dbe(rev_id, &(vino->rev_id))) {
4210                 printk(KERN_ERR "Failed to read VINO revision register\n");
4211                 vino_module_cleanup(vino_init_stage);
4212                 return -ENODEV;
4213         }
4214
4215         if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
4216                 printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
4217                        rev_id);
4218                 vino_module_cleanup(vino_init_stage);
4219                 return -ENODEV;
4220         }
4221
4222         printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
4223
4224         return 0;
4225 }
4226
4227 static int vino_init(void)
4228 {
4229         dma_addr_t dma_dummy_address;
4230         int err;
4231         int i;
4232
4233         vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
4234         if (!vino_drvdata) {
4235                 vino_module_cleanup(vino_init_stage);
4236                 return -ENOMEM;
4237         }
4238         vino_init_stage++;
4239         strlcpy(vino_drvdata->v4l2_dev.name, "vino",
4240                         sizeof(vino_drvdata->v4l2_dev.name));
4241         err = v4l2_device_register(NULL, &vino_drvdata->v4l2_dev);
4242         if (err)
4243                 return err;
4244         vino_init_stage++;
4245
4246         /* create a dummy dma descriptor */
4247         vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
4248         if (!vino_drvdata->dummy_page) {
4249                 vino_module_cleanup(vino_init_stage);
4250                 return -ENOMEM;
4251         }
4252         vino_init_stage++;
4253
4254         // TODO: use page_count in dummy_desc_table
4255
4256         vino_drvdata->dummy_desc_table.dma_cpu =
4257                 dma_alloc_coherent(NULL,
4258                 VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
4259                 &vino_drvdata->dummy_desc_table.dma,
4260                 GFP_KERNEL | GFP_DMA);
4261         if (!vino_drvdata->dummy_desc_table.dma_cpu) {
4262                 vino_module_cleanup(vino_init_stage);
4263                 return -ENOMEM;
4264         }
4265         vino_init_stage++;
4266
4267         dma_dummy_address = dma_map_single(NULL,
4268                                            (void *)vino_drvdata->dummy_page,
4269                                         PAGE_SIZE, DMA_FROM_DEVICE);
4270         for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
4271                 vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
4272         }
4273
4274         /* initialize VINO */
4275
4276         vino->control = 0;
4277         vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4278         vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
4279         udelay(VINO_DESC_FETCH_DELAY);
4280
4281         vino->intr_status = 0;
4282
4283         vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4284         vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
4285
4286         return 0;
4287 }
4288
4289 static int vino_init_channel_settings(struct vino_channel_settings *vcs,
4290                                  unsigned int channel, const char *name)
4291 {
4292         vcs->channel = channel;
4293         vcs->input = VINO_INPUT_NONE;
4294         vcs->alpha = 0;
4295         vcs->users = 0;
4296         vcs->data_format = VINO_DATA_FMT_GREY;
4297         vcs->data_norm = VINO_DATA_NORM_NTSC;
4298         vcs->decimation = 1;
4299         vino_set_default_clipping(vcs);
4300         vino_set_default_framerate(vcs);
4301
4302         vcs->capturing = 0;
4303
4304         mutex_init(&vcs->mutex);
4305         spin_lock_init(&vcs->capture_lock);
4306
4307         mutex_init(&vcs->fb_queue.queue_mutex);
4308         spin_lock_init(&vcs->fb_queue.queue_lock);
4309         init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
4310
4311         vcs->vdev = video_device_alloc();
4312         if (!vcs->vdev) {
4313                 vino_module_cleanup(vino_init_stage);
4314                 return -ENOMEM;
4315         }
4316         vino_init_stage++;
4317
4318         memcpy(vcs->vdev, &vdev_template,
4319                sizeof(struct video_device));
4320         strcpy(vcs->vdev->name, name);
4321         vcs->vdev->release = video_device_release;
4322         vcs->vdev->v4l2_dev = &vino_drvdata->v4l2_dev;
4323
4324         video_set_drvdata(vcs->vdev, vcs);
4325
4326         return 0;
4327 }
4328
4329 static int __init vino_module_init(void)
4330 {
4331         unsigned short addr[] = { 0, I2C_CLIENT_END };
4332         int ret;
4333
4334         printk(KERN_INFO "SGI VINO driver version %s\n",
4335                VINO_MODULE_VERSION);
4336
4337         ret = vino_probe();
4338         if (ret)
4339                 return ret;
4340
4341         ret = vino_init();
4342         if (ret)
4343                 return ret;
4344
4345         /* initialize data structures */
4346
4347         spin_lock_init(&vino_drvdata->vino_lock);
4348         spin_lock_init(&vino_drvdata->input_lock);
4349
4350         ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
4351                                     vino_vdev_name_a);
4352         if (ret)
4353                 return ret;
4354
4355         ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
4356                                     vino_vdev_name_b);
4357         if (ret)
4358                 return ret;
4359
4360         /* initialize hardware and register V4L devices */
4361
4362         ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
4363                 vino_driver_description, NULL);
4364         if (ret) {
4365                 printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
4366                        SGI_VINO_IRQ);
4367                 vino_module_cleanup(vino_init_stage);
4368                 return -EAGAIN;
4369         }
4370         vino_init_stage++;
4371
4372         ret = i2c_add_adapter(&vino_i2c_adapter);
4373         if (ret) {
4374                 printk(KERN_ERR "VINO I2C bus registration failed\n");
4375                 vino_module_cleanup(vino_init_stage);
4376                 return ret;
4377         }
4378         i2c_set_adapdata(&vino_i2c_adapter, &vino_drvdata->v4l2_dev);
4379         vino_init_stage++;
4380
4381         ret = video_register_device(vino_drvdata->a.vdev,
4382                                     VFL_TYPE_GRABBER, -1);
4383         if (ret < 0) {
4384                 printk(KERN_ERR "VINO channel A Video4Linux-device "
4385                        "registration failed\n");
4386                 vino_module_cleanup(vino_init_stage);
4387                 return -EINVAL;
4388         }
4389         vino_init_stage++;
4390
4391         ret = video_register_device(vino_drvdata->b.vdev,
4392                                     VFL_TYPE_GRABBER, -1);
4393         if (ret < 0) {
4394                 printk(KERN_ERR "VINO channel B Video4Linux-device "
4395                        "registration failed\n");
4396                 vino_module_cleanup(vino_init_stage);
4397                 return -EINVAL;
4398         }
4399         vino_init_stage++;
4400
4401         addr[0] = 0x45;
4402         vino_drvdata->decoder = v4l2_i2c_new_probed_subdev(&vino_i2c_adapter,
4403                         "saa7191", "saa7191", addr);
4404         addr[0] = 0x2b;
4405         vino_drvdata->camera = v4l2_i2c_new_probed_subdev(&vino_i2c_adapter,
4406                         "indycam", "indycam", addr);
4407
4408         dprintk("init complete!\n");
4409
4410         return 0;
4411 }
4412
4413 static void __exit vino_module_exit(void)
4414 {
4415         dprintk("exiting, stage = %d ...\n", vino_init_stage);
4416         vino_module_cleanup(vino_init_stage);
4417         dprintk("cleanup complete, exit!\n");
4418 }
4419
4420 module_init(vino_module_init);
4421 module_exit(vino_module_exit);