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