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