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